1 00:00:00,000 --> 00:00:04,590 In Python, we have this super cool function called 'super', 2 00:00:04,600 --> 00:00:09,110 and we can run this inside of our class methods, which is 3 00:00:09,120 --> 00:00:11,780 why we call it a method and not a function, because it does 4 00:00:11,780 --> 00:00:15,000 actually have some nice extras like the 'super' keyword, which 5 00:00:15,000 --> 00:00:16,800 we're going to explore in this video. 6 00:00:16,820 --> 00:00:19,910 So the 'super()' function is really only a function you're going to 7 00:00:19,910 --> 00:00:23,400 use in a method in a subclass, and that bubbles up 8 00:00:23,400 --> 00:00:27,000 to the original method that it's inheriting from. 9 00:00:27,000 --> 00:00:31,800 So a good example of this is going to be, let's do 'class One:', 10 00:00:31,800 --> 00:00:33,500 and I'm just going to use generic terms here. 11 00:00:34,900 --> 00:00:39,200 'def speak(self): print( 12 00:00:39,200 --> 00:00:42,200 "This is number #1")'. 13 00:00:42,900 --> 00:00:46,100 Now let's do 'class Two:', which is going to inherit from 'class 14 00:00:46,190 --> 00:00:50,530 'One', 'def speak(self): print( 15 00:00:50,800 --> 00:00:53,700 "This is #2")'. 16 00:00:53,700 --> 00:00:56,700 And when we instantiate both of these, we can do 'one = 17 00:00:56,700 --> 00:00:59,500 One()', 'one.speak()'. 18 00:00:59,530 --> 00:01:05,190 We can also do 'two = Two()', and 'two.speak()'. 19 00:01:06,500 --> 00:01:09,500 And that was an 'o', there we go. 20 00:01:09,560 --> 00:01:11,710 So this says, "This is #1", and, "This is #2". 21 00:01:11,770 --> 00:01:15,790 Now what if for whatever reason, in this one here, I wanted 22 00:01:15,800 --> 00:01:18,600 to execute whatever this is in here? 23 00:01:18,620 --> 00:01:20,630 Well, that's where the 'super()' function comes in. 24 00:01:20,640 --> 00:01:24,110 So if I get rid of 'one', and 'one.speak()', just rerun that. 25 00:01:24,120 --> 00:01:25,010 It says, "This is #2". 26 00:01:25,100 --> 00:01:28,170 But I also want this to be in here as well. 27 00:01:28,400 --> 00:01:32,400 How do I do that without actually explicitly writing that in there? 28 00:01:32,400 --> 00:01:36,000 Because if I change it in here, I'm then going to have to change it in here, 29 00:01:36,000 --> 00:01:38,500 and that can just be a nightmare when your application gets 30 00:01:38,500 --> 00:01:39,500 really, really big. 31 00:01:39,500 --> 00:01:43,700 What we can do instead is use this 'super()' function. 'super.', 32 00:01:43,700 --> 00:01:45,700 and then you give it the method name, 33 00:01:45,710 --> 00:01:47,360 so 'super().speak()'. 34 00:01:47,750 --> 00:01:51,590 So what this is going to do, is it's going to say, "Oh, I recognize 35 00:01:51,590 --> 00:01:54,320 that I am actually a subclass of 'class One', 36 00:01:54,860 --> 00:01:58,430 so I'm going to bubble up to that class and then run the 37 00:01:58,440 --> 00:02:01,470 '.speak' method", and that's going to go up to number 'One', 38 00:02:01,480 --> 00:02:03,270 or 'class One', run 'speak()'. 39 00:02:03,360 --> 00:02:07,160 So this will now say, "This is #1", as well as "This is #2". 40 00:02:07,190 --> 00:02:11,150 So let's go ahead and just rerun these cells, and we can 41 00:02:11,150 --> 00:02:12,300 see - "This is #1", "This is #2". 42 00:02:12,300 --> 00:02:15,770 And if I change it in here, rerun these cells, 43 00:02:15,770 --> 00:02:17,700 and sure enough, it works for us. 44 00:02:17,780 --> 00:02:20,900 Now, this is actually a terrible example because you're not 45 00:02:20,900 --> 00:02:24,100 going to use this with the 'print' method, probably never. 46 00:02:24,120 --> 00:02:26,790 But you might use this with some sort of logic. 47 00:02:26,800 --> 00:02:31,300 So let's implement something else in here. In 'One' let's implement 48 00:02:31,380 --> 00:02:36,390 a method that says, or is called 'total_tacos', because 49 00:02:36,400 --> 00:02:38,310 I don't know, I have tacos on the brain right now, I guess. 50 00:02:39,600 --> 00:02:42,200 And this is simply going to return 5. 51 00:02:42,200 --> 00:02:43,200 I have 5 tacos. 52 00:02:44,040 --> 00:02:49,750 And in this cell in 'class Two', I want 'def total_tacos' as 53 00:02:49,760 --> 00:02:52,120 well otherwise it's just always going to 'return 5 for me. 54 00:02:52,130 --> 00:02:54,430 But let's say I want to add new Tacos. 55 00:02:54,900 --> 00:02:59,900 So what I can do here, is say the 'total_original_tacos = 56 00:03:00,200 --> 00:03:02,900 super(). 57 00:03:02,900 --> 00:03:04,040 total_tacos'. 58 00:03:04,900 --> 00:03:12,100 I can 'print("Original tacos:", is going to be total_original_tacos)' 59 00:03:13,200 --> 00:03:14,900 And now I can actually work with this number as well. 60 00:03:14,900 --> 00:03:18,700 So this is going to be 5, because 'total_original_tacos' 61 00:03:18,760 --> 00:03:21,970 is whatever this function, this method up here is returning, 62 00:03:21,970 --> 00:03:23,100 so that's going to be 5. 63 00:03:23,100 --> 00:03:24,900 So it returns 5 from here, 64 00:03:25,400 --> 00:03:26,600 that gets called from here. 65 00:03:26,610 --> 00:03:30,140 'super()' says, "Bubble up to class number 'One' because I'm currently 66 00:03:30,140 --> 00:03:32,800 a subclass", and run that function. So let's go. 67 00:03:32,810 --> 00:03:39,260 'new_tacos = total_original_tacos + 3' more. 68 00:03:39,260 --> 00:03:43,000 So now I have 8 tacos. 'return new_tacos'. 69 00:03:43,460 --> 00:03:45,560 And let's also print that. 'print( 70 00:03:46,200 --> 00:03:49,300 "New total Tacos is going to be", nope, 71 00:03:49,300 --> 00:03:52,100 that's the wrong one, 'new_tacos'. 72 00:03:53,100 --> 00:03:54,500 So let's rerun these cells. 73 00:03:54,700 --> 00:03:57,100 And in this one, we don't want to 'speak' because that's not going 74 00:03:57,100 --> 00:03:58,200 to do what we want. 75 00:03:58,200 --> 00:04:01,100 This one is going to run 'total_tacos()', 76 00:04:02,700 --> 00:04:05,100 and so the "Original tacos" were 5. 77 00:04:05,100 --> 00:04:07,000 We can actually see that right here. 78 00:04:07,100 --> 00:04:11,500 And then we took those original 'tacos', and we added 3 79 00:04:11,500 --> 00:04:12,400 to those 'tacos'. 80 00:04:12,400 --> 00:04:16,399 And now our "New total tacos" is 8. 81 00:04:17,399 --> 00:04:19,800 And so essentially, what this did was it bubbled up to its 82 00:04:19,800 --> 00:04:20,899 parent class, 83 00:04:20,980 --> 00:04:23,230 it ran this function for us, 84 00:04:23,240 --> 00:04:26,350 we've returned whatever that value is going to be, and we 85 00:04:26,350 --> 00:04:28,399 can now work with that value. 86 00:04:28,399 --> 00:04:31,600 And that is how we use the 'super()' function. 87 00:04:31,640 --> 00:04:36,830 So this seems like not a super important feature right now, 88 00:04:36,840 --> 00:04:39,440 but when you get into larger frameworks that come with all 89 00:04:39,440 --> 00:04:43,240 sorts of pre-built functions and methods, and all sorts of 90 00:04:43,250 --> 00:04:45,370 things for you, you're going to use this a lot because you're 91 00:04:45,380 --> 00:04:47,470 going to want to leverage the code that someone else has 92 00:04:47,480 --> 00:04:50,200 already written with the 'super()' function, 93 00:04:50,500 --> 00:04:53,400 and then you can do some other stuff on top of that.