Mastering PyCharm Transcripts
Chapter: The Editor
Lecture: Object-oriented and class-based features
0:01 Let's explore the object-oriented features,
0:04 features for working classes and type hierarchies in PyCharm.
0:08 So over here, notice this is the code from my Python Jumpstart course
0:12 which is right here, we just git cloned that in one of the earlier videos
0:16 in case you're jumping in the middle,
0:18 just go here check out app seven final, that's what we're working with.
0:22 Over here we're importing a couple of classes
0:26 a Wizard, a Creature, a Small Animal and a Dragon,
0:28 and we'll go check this out in a second
0:31 but the creature is the base type
0:33 and then we have a Small Animal and a Dragon,
0:36 both deriving from Creature and then Wizard as well.
0:40 So let's go over to this actors thing
0:43 we can use double search or so, just hit A and look,
0:47 there's actors right there,
0:49 obviously, it's small enough we could click over and find it
0:51 but notice, we have this type hierarchy, here we've got our Creature
0:56 the Creature is kind of the base type
0:59 and it implicitly inherits from object in Python 3, it's as if we wrote this,
1:04 but we don't have to because it's Python 3,
1:09 so down here we have things like the __init__
1:13 and notice this is saying that this is overwriting
1:16 or is overwritten in Dragon for example among other places.
1:20 This class is derived from by Wizard, Small Animal and Dragon,
1:26 that's pretty super cool, right,
1:28 and this is part of the Python data model, this actually comes from objects
1:31 we're overwriting this from object.
1:36 Now, here, this get defensive roll
1:39 this is itself overwritten somewhere else
1:42 so let's play the game real quick,
1:45 now remember, we could just click here and run it
1:46 but I'll show you one other way to run our code
1:50 just because sometimes this has console colors and whatnot
1:54 and you just want it in a proper terminal
1:56 so I could do this copy, copy path open this up, clear that out
2:01 we'll just say Python 3 and boom, there it goes,
2:05 well I guess it doesn't have console colors but that's all fine.
2:07 So what we could do is we could look around
2:09 and we could see that there is a Toad of a certain level,
2:12 a Tiger, a Dragon, an Evil Wizard
2:15 and then we could say I would like to attack the Bat of level 3
2:18 that has appeared from the dark and foggy forest.
2:21 The Wizard has handily triumphed over the Bat,
2:24 now a Tiger a is here, we could attack it,
2:25 a Dragon— we've been defeated and so on.
2:29 The idea is we play this game, it's very simple
2:32 but you basically have these attacks that the Wizard can do
2:35 and all the creatures can have a defensive roll,
2:37 some of them like we have over here
2:40 the Small Animal and the Dragon have different types of defense
2:45 than the standard Creature,
2:47 well, we can use this to discover information about the type
2:51 but we can also use this to navigate,
2:53 so watch this, if I want to go over here
2:55 to where this is all written by Dragon I click,
2:57 boom, I'm in the Dragons __init__ where it's overwriting the Creatures
3:01 and then, of course, we are doing this super here.
3:04 We're also capturing whether the Dragon breathes fire,
3:07 capturing its level of scaliness, things like this.
3:10 This works in reverse too, get defensive roll, that says
3:14 this is overwritten from Creature, click on that, it takes us back to Creature.
3:17 It even, and so much as we can understand
3:20 information about the standard library works there as well
3:24 so this will take us to the representation method in object
3:29 but because it's implemented in C you don't know so much,
3:33 not all of the standard libraries implemented in C though
3:35 and if there was some piece that was written in Python
3:38 it would take us straight to that and that would be awesome.
3:40 So you can see whenever we're working with these type hierarchies,
3:42 we get nice little tips about what we're doing
3:46 and this can let you know like should you call
3:48 things like the super base method or not.
3:52 So here the small Creature is like half as defensive as a standard Creature
3:56 because it's small and wimpy, right
3:58 that's kind of what this implementation says.
4:00 For the Dragon, there's a bunch of different modifiers that we can have here,
4:05 we tried a few attempts in the course that actually wrote this code
4:08 to come up with it and talk about some of the expressions here
4:11 but basically, whether you're scaly, whether you breathe fire
4:14 these affect how defensive you can be as a Dragon.
4:18 You can see they are pretty tough to beat
4:19 a scaly fire-breathing Dragon is super hard to beat in this game.
4:23 But, hopefully, that gives you a good sense of some of the objects or features
4:26 the other things that you can notice maybe is if I type self.
4:30 I get Dragon features as autocomplete,
4:34 I get Creature features from the base type, and I get object features,
4:39 so you immediately get things like autocomplete
4:41 throughout the type hierarchy as well— pretty awesome.