Python 3, an Illustrated Tour Transcripts
Chapter: Classes and inheritance
Lecture: Walk-through: super()
Login or purchase this course to watch this video and the rest of the course contents.
0:00 In this video we're going to look at the super test assignment. Here's the assignment.
0:07 It has a function called test Mario and let's just run it and make sure it works,
0:12 right click and say run super test and I get one fail, that's because there's one test here. I get an import error line 26. Okay, that looks okay.
0:24 I'm going to change my size here a little bit so we can get a little bit more code on the screen.
0:29 This test has a table that lists people from Mario Brothers, the game. So across the top are Mario Brothers, down the left hand side are various skills
0:39 and here are their levels or scores for those skills. So it says create a base class called character that has speed, jump and power methods.
0:47 They should each return 2. Create subclasses for Mario and Luigi that use super to call the parent class method
0:53 and increment the value by the appropriate amount before returning it and put this function in a module called py3code.py
1:01 It says function here, it should say put this code in a module called py3code.py There's a py3.code guy right here
1:10 and it says put your super stuff right there. I'm going to right click on this and say move right
1:17 and I'm going to adjust my size here bump my fonts down a little bit they are 20, I'll just put them down to 16 for this assignment.
1:36 Okay, so we need to make a class called character so class character. and it needs to have 3 methods, it needs to have a speed, jump and power method.
1:51 So to make a method here I say def and I'm just going to call it speed and PyCharm automatically puts itself in there for me
2:00 and I'll just say return 2 and I'll just copy these, and I'll indent them. And I'll change the name, so this is speed,
2:16 this should be jump and this one should be power. Okay, so there's our base character and he has certain skills.
2:29 Let's make a Mario subclass and say class Mario and I put the parent class in the parentheses here. So the parent class is character.
2:40 And I need to make a speed method and he's going to have self as the first parameter and what the assignment wants me to do is
2:50 it wants me to rather than just saying return Mario has a speed of 4, it says it wants me to say value or some intermediate variable is super.speed.
3:04 And this should return 2, and then instead of returning 2, we're going to return value plus 2, that should get us 4.
3:11 And we'll do a similar thing for the other guys. So this should be instead of speed here, we'll say this is jump, we'll change this to jump.
3:25 And this one should be power, we'll change this to power. So this shows how we call the parent class method here.
3:37 We just say super and that gives us access to the parent class and then we call the method on that. Let's run this and see if it works.
3:45 So I need to run the test code over here, not that py3code. So I'm going to run super test. And I get an import error cannot import Luigi.
3:54 So apparently, I need to make a Luigi as well. Let's do a Luigi as well. Luigi, and his speed is going to be 1 greater,
4:13 his jump is going to be 3 greater, and his power is going to be 1 greater. Okay, let's run it again and see if it works now.
4:21 Okay, it looks like it worked. So let's look at the test here really quickly.
4:27 It's just creating an instance of Mario and calling the speed method on Mario.
4:31 It's asserting that character is in the __bases__ attribute of the Mario class.
4:36 So when you create a class there's a __bases__ attribute the list the base classes. It's asserting that the speed is the correct value
4:44 and then it's making a little function here called speed that looks like a method and it's monkey patching that m for speed
4:52 to make the base be return 5 instead of the base speed of 2 and then it's calling Mario again to see that Mario speed now returns 7, 2 more than that 5
5:04 and Luigi speed returns 1 more than that 5. Okay, hopefully you understand a little bit more
5:15 about the super method or super function built-in in Python when you're in a method and you want to call a parent method
5:22 rather than explicitly saying, in this case rather than saying character.speed, we say super, that allows us to get access to character.
5:30 but if we change this or refactor it later where character is no longer the base class super will do the right thing.