Welcome back.
In the previous video, I showed you how to call a method from a subclass of the parent class.
So in our case, we did user dot attack.
In order to run the attack method and user.
Now I've removed the attack method from now because I want to try something different.
I want to create a init function in here and this init function will take self.
And then let's say that the user will receive, let's say email.
Because all the users of our game need to have emails.
And in here we'll just say self dot email will equal the email.
Nice and simple.
Now in here.
If I was to run this code and let's remove user dot attack for now.
So we have the same wizard we just added this or sorry this init.
And we have our wizard won.
So if I run wizard here and let's say we want to run Wizard one dot email.
If I click Run.
Hmm.
I get.
Wizard object has no attributes.
Email.
Hmm.
But I thought the user is going to be called when we create the wizard, right?
When we instantiate Wizard One.
Well, the thing is, although we might inherit, let's say, for example, if I do dot, we might inherit
the.
Sign in.
From the user.
And if I click run, we see that we inherit that.
The wizard already has an init function already a constructor, right?
So how can we solve this?
Well, one way is to just add email here, right?
And just have the email.
Attribute just right down here.
But what's the problem with this?
Well, it's inefficient, right?
Because if I create an Archer class, I'll have to do the same thing.
And when we know that all these attributes are going to be the same on all of our subclasses, we want
to do it here.
So what's a more efficient way of doing this?
Well we want to call the init method of the user from inside here.
Right.
Because every time we instantiate a new wizard, well this init function is going to be called.
So the way to do that is to say user dot.
Underscore underscore in it.
And then give it the self.
And then email.
And then the email is something that we pass in.
So that if I do go down here, I can say.
Merlin at gmail.com is going to be the email.
So that now if I do, let's say.
Dot email for Wizard One and I click Run.
I get Merlin's email once again.
All we did was we called the init method.
Of the user inside of the wizard.
So when we instantiated a new wizard, we ran this function and inside of this function we ran the user
dot init and passed in the email that we passed in as an argument in here.
Now, this is one way of doing it.
And then there's another way, and it's called super.
And it's a key word.
As you can see, it's being highlighted here.
And all we do is run super.
And Super is referring to the superclass or the class above wizard, which is user.
And this is actually a new addition as of Python 2.2.
With Super, we actually no longer need the self, so our code looks even cleaner and all we pass is
this and if we click run.
We still get the right answer.
Super.
Allows us to refer to user this way and not have to worry about passing self.