In Python, we have this super cool function called 'super',
and we can run this inside of our class methods, which is
why we call it a method and not a function, because it does
actually have some nice extras like the 'super' keyword, which
we're going to explore in this video.
So the 'super()' function is really only a function you're going to
use in a method in a subclass, and that bubbles up
to the original method that it's inheriting from.
So a good example of this is going to be, let's do 'class One:',
and I'm just going to use generic terms here.
'def speak(self): print(
"This is number #1")'.
Now let's do 'class Two:', which is going to inherit from 'class
'One', 'def speak(self): print(
"This is #2")'.
And when we instantiate both of these, we can do 'one =
One()', 'one.speak()'.
We can also do 'two = Two()', and 'two.speak()'.
And that was an 'o', there we go.
So this says, "This is #1", and, "This is #2".
Now what if for whatever reason, in this one here, I wanted
to execute whatever this is in here?
Well, that's where the 'super()' function comes in.
So if I get rid of 'one', and 'one.speak()', just rerun that.
It says, "This is #2".
But I also want this to be in here as well.
How do I do that without actually explicitly writing that in there?
Because if I change it in here, I'm then going to have to change it in here,
and that can just be a nightmare when your application gets
really, really big.
What we can do instead is use this 'super()' function. 'super.',
and then you give it the method name,
so 'super().speak()'.
So what this is going to do, is it's going to say, "Oh, I recognize
that I am actually a subclass of 'class One',
so I'm going to bubble up to that class and then run the
'.speak' method", and that's going to go up to number 'One',
or 'class One', run 'speak()'.
So this will now say, "This is #1", as well as "This is #2".
So let's go ahead and just rerun these cells, and we can
see - "This is #1", "This is #2".
And if I change it in here, rerun these cells,
and sure enough, it works for us.
Now, this is actually a terrible example because you're not
going to use this with the 'print' method, probably never.
But you might use this with some sort of logic.
So let's implement something else in here. In 'One' let's implement
a method that says, or is called 'total_tacos', because
I don't know, I have tacos on the brain right now, I guess.
And this is simply going to return 5.
I have 5 tacos.
And in this cell in 'class Two', I want 'def total_tacos' as
well otherwise it's just always going to 'return 5 for me.
But let's say I want to add new Tacos.
So what I can do here, is say the 'total_original_tacos =
super().
total_tacos'.
I can 'print("Original tacos:", is going to be total_original_tacos)'
And now I can actually work with this number as well.
So this is going to be 5, because 'total_original_tacos'
is whatever this function, this method up here is returning,
so that's going to be 5.
So it returns 5 from here,
that gets called from here.
'super()' says, "Bubble up to class number 'One' because I'm currently
a subclass", and run that function. So let's go.
'new_tacos = total_original_tacos + 3' more.
So now I have 8 tacos. 'return new_tacos'.
And let's also print that. 'print(
"New total Tacos is going to be", nope,
that's the wrong one, 'new_tacos'.
So let's rerun these cells.
And in this one, we don't want to 'speak' because that's not going
to do what we want.
This one is going to run 'total_tacos()',
and so the "Original tacos" were 5.
We can actually see that right here.
And then we took those original 'tacos', and we added 3
to those 'tacos'.
And now our "New total tacos" is 8.
And so essentially, what this did was it bubbled up to its
parent class,
it ran this function for us,
we've returned whatever that value is going to be, and we
can now work with that value.
And that is how we use the 'super()' function.
So this seems like not a super important feature right now,
but when you get into larger frameworks that come with all
sorts of pre-built functions and methods, and all sorts of
things for you, you're going to use this a lot because you're
going to want to leverage the code that someone else has
already written with the 'super()' function,
and then you can do some other stuff on top of that.