Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 7: Wizard Battle App
Lecture: Initializing classes and creating objects

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We have this concept of defining a creature as a thing in memory and wizard as a thing in memory
0:08 but they don't really do anything or have any behaviors or data, so let's go over here and look at this, let's look at the creature first.
0:15 So what might the creature have, well, maybe the creature is going to have to have a level, and let's have it have a name,
0:22 so we can say something like toad of level 7, something like this. So there is actually several ways to add those features as data to this class.
0:32 And the way that I am going to show you will generate what are called instance attributes or instance variables,
0:38 and these are only going to be tied specifically to a particular version or particular create instance of the creature,
0:45 so the data related to this creature will not at all be shared with this creature, there is way to have sort of what you think of a static shared data
0:53 but we are going to use what is called a magic method. Now, to add methods, magic or otherwise to classes just like normal methods you say def
1:01 but you have them indented into the class body. Now, I could have a method that is called run away,
1:07 like this and then that would be a standard method there, but we are going to use what I said a magic method,
1:13 so magic methods start with double underscores often called dunder, and you can see there are a lot of them, so here you can see length,
1:24 if we had some kind of collection and we wanted to talk about how many items were in it,
1:29 we could implement __len__() and when somebody says len (our object) it would implicitly call this method.
1:36 If we wanted to change how our creature appeared when you just said print creature, it was no special formatting we could implement __str__()
1:44 and then we would get some kind of special string representation. But the one we are looking for is __init__().
1:51 So here there is something a little bit unusual in this method, notice when I hit enter there, PyCharm put this self and this self has a color,
1:59 while other variables in here, parameters, they wouldn't have this purple color,
2:05 remember, the class is the blueprint and then there are the object instances and each one of them has their own special copy of data
2:11 and that's what this self represents, this is the pointer to the one that has been used to call this method,
2:17 so in this case it's the pointer to the object that is actually being created in init. so we want to have a couple of things,
2:23 we want to have a name and a level, remember this is kind of like dungeon and dragons type thing,
2:28 so we have a name and a level that you have to pass in, these are not optional parameters on the initializer for creatures,
2:35 you will see in just a minute, and then the way we define instance variables is we use this self parameter and we just say something like
2:41, and self.level=level. Now, these don't have to match, let's just change this to like the level to make it really obvious
2:52 that there is no real name relationship between them, but self. this is actually the creation,
2:57 the definition of name attribute or field in the creature class. It's highly discouraged to do this outside of the init method.
3:05 Now, while we are at it, let me show you a cool PyCharm trick so if I had another thing here like let's say experience or whatever,
3:12 notice that it's not assigned to any member variable or used in any way, so I can come over here in PyCharm,
3:20 I can hit enter and say add this field to the class. And bam, it'll go here and automatically do what you just saw me do,
3:26 so we are not actually going to have this experience but you can use PyCharm to kind of automate this, it's very nice.
3:31 Now, it turns out that the wizard is going to be very similar so let me copy this over here and I'll just change this form the level to level, like so,
3:42 so now we should be able to create our wizard and create our creature and they should have data associated with them,
3:48 the next we are going to give it behaviors like maybe it has an attack method here and it can attack other creature, or just a creature,
3:56 and down here we'll put something that has to do with attacking a creature. So I told you that these are required,
4:03 if I try to run this app again, it's going to crush, and it crashes right here and says, wow,
4:08 we are missing two required positional arguments, name and the level, I can do things like give it default value like
4:15 so I could say by default the level is 10 unless they specify it then it will be something else.
4:20 So then you'll see, only one required parameter is missing, because if you don't specify the level, hey it's 10 so all good.
4:26 That didn't actually make a lot of sense in our particular game here, so I'll take that back away.
4:33 All right, so let's go figure out what's wrong and fix this, so I can just click right here and navigate right to where the error was
4:39 and you see PyCharm has this kind of lit up and color here saying not such a good idea, all right so what do we have here, we have the name,
4:47 remember, let's see we have a toad and the level of toad was quite low, and we had a tiger and the level of tiger was modernly high,
4:55 I think we had the bat, the level of the bat was not particularly high it was 3, we had a dragon and the level of the dragon was pretty high
5:02 and we had the evil wizard, so now our list of creatures will actually contain particular copies of the creature object, five of them,
5:13 and each one of them will have a separate name and a separate level tied together. one thing that is a little bit off here is using this creature class
5:22 for a wizard when we also have a wizard class, so let's leave it like this for a minute and we'll get back to it, so now here we have the same problem,
5:30 we have exactly the same requirements to create this, so let's say this is Gandolf, and his level is 75.
5:36 Ok, let's run this and see if we can get down pass line 27. It looks great, let's actually run it in the debugger
5:43 and we'll have a look at these in memory. So here we are at the break point, we've got our local variables
5:48 and we have a hero and you can see we can open this up, we have this hero has a level 75 and his name is Gandolf,
5:54 and we actually have a list of creatures, and you see there is various creature objects,
5:59 these are manifestations or things created from the blueprint of creature, there are different memory addresses,
6:04 everything in Python points to somewhere in memory and so the elements of this list all point to different places
6:10 and the debugger happens to show you that if you really care about it. We can open this up and see there is our bat,
6:17 there is our tiger, there is our toad and so on. So we spoke a little bit about magic methods,
6:23 and you can see that this is not the most amazing experience for looking at what is in this list of creatures
6:29 and that's not the only place it showed up, if I were to come here and just say print the creatures, and I run this,
6:36 you can see basically I have the same output here, there is some kind of actor.creature at these various addresses.
6:43 And we can use the magic methods to change how this works. so my creature, I can down here and I can say define __repr__(), for representation,
6:52 and then I could just say return some kind of string so let's say creature of the name of level- something there we'll do a little format like so
7:02 and so when you say self when you refer to the variables of the particular instance of the type of class like so
7:09 the particular creature here, I'd say, and self.level. Of course maybe format instead of forma, have a little better chance there.
7:18 So we are going to return a string that could be used in various places, so now if I run this you can see we have a creature toad of level 1,
7:25 maybe like so or something. Creature, toed of level 1, creature tiger of level 12 and so on,
7:36 so we are only getting started with our classes and modelling the actors in our game,
7:41 but you can see we are already after a good start we have a list of creature objects, they have their particular data associated with them,
7:50 and we have our hero which is a wizard named Gandolf of level 75.

Talk Python's Mastodon Michael Kennedy's Mastodon