Super-Duper!4:36 with Kenneth Love
We need a way to make use of the code that lives in our classes' superclasses. I bet there's a way to do this!
super() function lets us call a bit of code from the parent class inside our own class. This is really helpful when you need to override a method from the superclass, defining your own version, but keep the effects of the parent class's version of the code.
For more about
super() than you probably want to know at this point, check out this blog post from the excellent Raymond Hettinger.
You're doing great so far. 0:00 Object-oriented programming isn't the easiest thing to pick up, but 0:02 you're already creating classes with attributes and methods. 0:04 And you've used inheritance to reduce the amount of code you have to write. 0:07 But when we made our Thief class inherit from our Character class, 0:10 we lost the ability to set the sneaky attribute when we instantiated an object. 0:13 There's a great way to do this in Python though using a function named super. 0:17 Remember how we called the class we inherit from the super class? 0:21 Well, super lets us use the code from our super class on demand, 0:24 even after we've overwritten stuff in our subclasses. 0:28 Let's go back to workspaces and try out the superlative function. 0:31 Since we want to customize the initialization of our Thief, 0:34 that means we have to override init. 0:38 That's where we're gonna use super. 0:40 Before we can do that though, we have to redefine the method in our sub class. 0:42 So in this case, we're running init so we have to define init. 0:47 And we're gonna take self, name, and kwargs, just like above. 0:52 I'm just gonna put in pass for right now. 0:57 So if we were to run this right now, our Thief actually wouldn't have 0:59 the name attribute and stuff of the character from above. 1:03 Because we've passed on this init, 1:06 we haven't actually continued with the super class as init. 1:08 We want to make sure that all of the actions of our character init take place. 1:11 We can control when that happens though. 1:16 But we do that by controlling where we put our call to super. 1:19 For instance, we want to make sure that our instance has the sneaky attribute 1:24 on it, right. 1:27 So we want self.sneaky = True. 1:28 Now we can put that before we set the name and kwarg attributes. 1:31 Or after we set the name and kwarg attributes. 1:37 In this situation, it doesn't really matter. 1:43 We're not doing anything in this init or 1:46 in the super classes init that depends on the other. 1:48 Well, except for one thing, if someone passed in a sneaky kwarg, 1:50 it could override what we want to set if we had the super after we set self.sneaky. 1:54 I think we want to prevent that, so 2:00 we should call super first and our current call here where we're just using the word 2:02 super isn't gonna do what we want it to do. 2:07 We need to provide a bit more data. 2:10 When we use super, we have to include the method name and 2:14 its required arguments too. 2:17 This calls the same method, 2:19 whatever method is here after the dot using our instance. 2:21 So it's as if we were creating an instance of the parent class. 2:27 So after this method call is done, our instance will have a name set and 2:31 any kwarg provided attribute will be set too. 2:34 And since we do the sneaky assignment after that, it's always set to true. 2:37 If we were to go into the shell and import our Character and make a Thief, and 2:42 we set sneaky to False, or sneaky to None, or sneaky to abc, 2:45 it would still always come out as True. 2:49 In our old version though, I could set sneaky when I created an instance. 2:53 Our Character's init method doesn't allow that keyword though. 2:57 At least, not explicitly. 3:00 But subclass methods don't have to have signatures, 3:02 which is the arguments that it accepts, that exactly match the superclass. 3:05 So subclasses can take different arguments than their parent classes. 3:10 Let's use that and let's add a sneaky parameter to our init. 3:15 A parameter for sneaky not, you know what I mean. 3:19 So let's say sneaky = True,and then down here sneaky = sneaky. 3:23 Right, it equals whatever comes in. 3:29 So notice that we're not passing the sneaky parameter to the superclasses init, 3:31 we didn't add it in here. 3:35 It doesn't expect to have a sneaky argument and frankly, 3:37 it doesn't want that sneaky argument. 3:41 But we can go ahead and use it in our own init class. 3:44 So now we have actual control over the sneakyness of our Thief, 3:48 let's try this out. 3:52 From characters import Thief, kenneth = Thief("Kenneth", 3:58 sneaky=False, and then just to make sure that init 4:04 is still working, let's add clever = True. 4:09 At least I'd like to think my character would be clever. 4:13 So if I do kenneth.sneaky, it's set to false, and 4:16 if I do kenneth.clever, it's set to True. 4:18 Awesome. 4:22 Now we have all the functionality that we had before. 4:22 Super seems pretty straightforward? 4:26 Most of the time it is, but 4:28 it has some hidden abilities that can complicate things. 4:29 We'll look at another usage or two of it as we go on though, so 4:32 you'll be fine with it by the end of the course. 4:34
You need to sign up for Treehouse in order to download course files.Sign up