Effective PyCharm Transcripts
Lecture: Refactoring class methods
Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well now we've got our app startup looking a little bit nicer. Let's go look at the various classes that are involved.
0:06 Remember we've got our creature wizard, the small animal, the dragon and so on. Now one of the things that we do when we run this program is we might
0:18 Show Something Like This. A Tiger of the Level 12. And if we look around we'll see there's a total level one,
0:24 a tiger, level 12, a bat of level three and so on. Where is that coming from? It's coming from this repr method. So here you can see is a dragon,
0:36 such and such of level such and such. But what we'd like to do is actually solve a problem that is very common in code. And that's duplicate code.
0:46 So notice the small animal says it's a small animal, such and such. Down here we have a dragon and it says basically the same
0:55 thing. This bit right here is different. But other than that it's basically the same. Could we fix this? Sure,
1:02 we could, we could come down here and we could just go to the type of self, this is going to be the dragon and we can sit come down
1:11 here and go down __name like that and that's going to show exactly the same thing, say dragon in this case.
1:18 But if that code will run inside of a small animal that would say small animal If it were run inside a wizard,
1:23 it would say wizard. So maybe this duplicate code here is not really necessary. Let's go highlight this and we can re factor,
1:33 what we want to do is we're gonna pull members up, pull up means go to the base class. We want to take this and just put it in the base class.
1:40 So everything that derives from creature wizard, small animal, dragon and so on. They can get that and use it.
1:48 So here I can choose what things to pull up, right? We could do multiple methods and so on.
1:52 But this is the one we want and we could figure out where to put it There's only one object or one type in our type hierarchy here.
1:59 So it's just going to go there to notice the reprr method is gone from the dragon and on the creature we now have this nice generic one that we wrote
2:11 PyCharm is not going to make a big enough guess or assumption that we can just drop it from all of them.
2:16 Right, But we know that this is going to actually be fine to take away this repr from all of these and now we'll be able to use them just from
2:24 the base type here. Let's make sure we're using it exactly. We go over to the program when it starts up,
2:34 let's go to a little game loop and here we'll just print rubber permanent. I'll say hero like this. He's a rapper. Wizard. Gandalf of Level 75.
2:50 So it works perfectly for a wizard and you can imagine it's going to work just fine for the dragon and so on.
2:57 All right. So if we were saying like creatures of -2, that should be the dragon. We go dragon just has the name dragon. So I guess it's duplicate here,
3:08 But dragon dragon of level 50 were able to use this pull members up to take
3:13 this function, this method from one of the derived classes and put it into the
3:18 base class in a nice generic way that all the other objects in our type arching you can pick it up and run with and we were able to remove that
3:26 code duplication throughout these different classes and that's a good thing.