Welcome back, everyone, to part four of our discussion on object oriented programming, or we're talking
about inheritance.
Often Jenga will have classes with many useful methods and attributes already defined for us.
This is kind of the whole point of using Django is that they've already done the hard work of defining
many different Python files, modules, packages and classes within them.
What we would like to do in order to create our own custom sites is create our own derived variations
of those classes that Django provides for us through the use of something known as inheritance.
Inheritance allows us to pass in an existing class into our new custom class, which means we get to
inherit the use of the methods already defined within the parent class, including the in its method.
This saves us a lot of time, and it's going to be a very common syntax in Django that we're going to
discover as we use Django more and more.
So what we're going to do in this case is explore a couple of ideas around inheritance.
First, we're going to discover how to create a parent's class.
This is officially known as a base class.
Then we'll discover how to inherit from that parent class to create a child class officially known as
a derived class.
Then we're going to discuss how to use the inherited methods within the derived class, how to add new
methods, and then how to overwrite derived methods.
Let's get started.
All right.
Here I am inside our code editor.
I'm going to create a very simple class, which is called a person class.
And we're going to say that this person class, when you actually create a new person, the init method
is going to require a first name and last name to create a new person.
So we're going to go ahead and say, here it takes itself.
And then it takes on a first name and it takes in a last name.
And we're going to sign those provided two attributes by the same name.
So.
The first name is equal to the provided first name and then self thought last name is equal to the provided
last name.
Then we're going to do is add in a couple of simple methods.
One method is going to be super simple.
It's just going to say hello.
And self.
And this is just literally going to print out.
Hello.
Then I'll have a slightly more complex method here, which is going to be called Report Peasant Self
here and report.
It's just going to print out this person and who they are.
So using an f string literal here, I'm going to say I am.
And then in curly braces, I can say self that first name.
SpaceX, another set of curly braces to insert a variable here and then self-taught last name.
Don't forget to use self here.
Otherwise, you won't actually be able to connect to the attributes the fine if you just say first name
and last name down here.
It would expect you to pass in first name and last name.
It's a report.
So this is a pretty simple class again.
It's just the person who has a first name and last name, and then this person class has two methods
associated with it.
The ability to print out hello and the ability to report who they are.
Notice both of these are simple print function calls.
Typically, we'd be using something like return.
But for simplicity here, when I'm running these files, I'll just go ahead and print out the results.
So let's create an instance of a person I will say X is equal to an instance of person that requires
a first name and last name.
So let's have that be John Smith.
And then what I'm going to do is say X, that's.
Hello.
Open close parentheses.
And then I'm going to run this code.
Oops!
And I get to see the output here is hello.
And let's try a report to make sure that's also working.
Open close parentheses, because it's a method.
Run that.
Now I see.
I am John Smith.
So clearly, I'm able to, as we've discussed, create an instance of the person class, the find them
of a first name and last name and then use methods either just hello or methods that actually use the
attributes similar to the way we calculated the area and perimeter of a circle.
Now let's imagine I'm going to now create a new class, which is kind of similar to a person, but I
also wanted to have other functionalities through different method calls.
What I can do is I can inherit some of the methods from the person class or really all of them.
So what I'm going to do here is I'm actually going to use Visual Studio Code Editor to collapse this
person class.
Just keep in mind, you can see I've collapsed based off the line numbers here on the left just to save
space on the screen.
Later on, I to show you how to import from other Python files so you don't have to have some huge Python
file.
Now I'm going to create a new class and this is going to be an agent class.
Agents are going to be really similar to a person class.
However, we want them to actually have this method where if I give them the correct passcode, they'll
report back, Hey, I'm a secret agent.
So we're to say class agent and we are going to inherit a person class.
And not how we do that, I simply pass in person.
Also, keep in mind, I'm not passing in person, open close parentheses, just saying person, not
like this.
OK, so we pass in person, and the act of passing in an instance of the person class now means I will
have access to the methods of the person class within agent, which is super useful because now I just
need to worry about the methods that I care about that are specific to agent.
So let's go ahead and create a method here called reveal and reveal takes and self, but also takes
in a passcode.
And we'll say if the passcode is equal to one, two three.
I will go ahead and print.
I am a secret agent.
Else, what can we do here?
I can go ahead and print.
I am.
And then say, self-taught first name and self-taught last name, however, remember I already had a
method really similar to that called report.
So because I inherited person, I can actually now use report anywhere I want.
I could say self-taught report to save myself some time.
So I'm certain instead of this, I'm going to say else self-taught.
Report.
Open close parentheses notice report is not defined anywhere within agents.
What it is, it's it's using the inherited person class to understand that self.
That report means call person that report, which I have up here.
OK, so I'm going to save this.
And let's create first an instance of person just to show you that they're not really being mixed and
matched upwards, they're only going through the inherited class.
So if you create a person, you can say X that report run this python example that pie and forgot to
define last name and first name, let's say again, John Smith here.
Save that.
OK, now let's go ahead and run this, OK, so I see I am John Smith.
Now let's try to call reveal we should expect a problem because I'm trying to call reveal on an instance
of person.
However, reveal is only defined for agent, which does inherits from person.
But this really only goes one way.
This relationship is just agent can now use the stuff with in person, but person has no real association
with agent.
So now if I were to save this, in fact, Visual Studio code tries to warn you by the fact it's not
actually coloring this method.
We should be able to see an error here.
And it says Attribute error person object has no attribute called reveal, essentially warning you,
hey, there's no method or attribute called reveal.
Now let's try this by creating an agent so instead of person here.
I'm going to call agents and an agent has to be constructed the same way a person does, which is to
say it inherits its in its method to construct an agent with a first name and the last name.
So I'm going to create an instance of agents called John Smith.
And let's actually call hello on this agent, know how this actually should work, because the coloring
is different here.
And the most important thing to note here is that hello is not directly defined with an agent.
That's a method agent is inheriting from person.
So if I run this code, I see I get back.
Hello.
I am also able to run report, which is another method the fine of then person.
And I get back.
I am John Smith.
Now what I'm going to do is use the method that is only available for agents, which is reveal, remember,
reveal those require a passcode.
If I just run it like this, I would expect an error saying, Hey, passcode not provided.
So it says Hey, required positional argument passcode.
Let's actually provide the correct passcode, which is reveal passing in one two three there.
I run this ad out, says, Hey, I am a secret agent.
OK, so just to be clear of what's going on here, I have the methods available inside my person class
by passing in person class into agents.
I have no access to all these methods and I can add on additional methods that I only want to be specifically
associated with an instance of this child class, otherwise known as a derived class.
Now, let's imagine I want to actually change what report does within agents.
How do I do that?
It's actually easy.
You can simply overwrite the inherited methods by saying the F and in the name of the method you want
to overwrite, you essentially redefine it like report.
Let's say a secret agent should probably not just report back their name John Smith.
Instead, we're going to say report for an agent is simply saying, I am here.
Instead of saying I am John Smith.
So now when we say reveal and we were passing the wrong passcode, we would expect report to simply
say, I am here for the case of an agent.
So if I run this, I won't get anything different.
He'll still say I'm a secret agent, but let's say the code is now, let's say one two three four.
We i that in wrong.
I run this and it simply says, Now I am here.
It no longer says I am John Smith.
And in fact, if we say x dot and simply call report now, it just says I am here, and that's a way
you can overwrite old methods in your child or derived class.
You just call that method and then overwrite it so you have the ability to use all these methods.
And then if you want it to, you can add new methods and overwrite the existing methods.
The last thing I want to point out here is there may be some instances where you actually want to add
in more attributes beyond first name and last name.
We ourselves won't really see this of jingo, but I do want you to be aware of the syntax for this.
If in fact you do need to add in more attributes than what is provided for us with the parent class.
You would essentially start overwriting the in its method itself, you would say underscore, underscore
in its underscore, underscore and self and then say first name, last name.
And let's imagine for some reason, an agent also needs a code name.
Now I still am inheriting person, so if I want to create an instance a person and then just add on
these attributes, the syntax for this is to say person dot underscore underscore in its underscore,
underscore and then pass in self.
First name, last name, and then we tack on the other attributes that we added here, like code name,
I would then say self code name is equal to code name.
I should point out and be clear here, this isn't really something we do that often within Django,
but it is possible.
And the reason you're doing this is if you have more attributes that you yourself need to add beyond
what's provided for you by the base class.
So then when you define an agent, it would also ask you for a code name like Mr. X or something.
And we can see what report is that actually shouldn't change anything.
So we see still here I am John Smith.
I think we said here report Oh, we actually didn't overwrite anymore.
I deleted that.
Let's actually just ask for its code name.
So I'm going to print out the code name Prince X thought, let's say first name.
And now let's also print out X thought.
Codename.
Save those changes, and now we see, OK, first name.
Is this give ourselves a little more space so we can see the whole thing?
First name is John and then codename was Mr. X.
All right.
So again, as a quick overview, you create the original class, have the init method.
Any other methods you want that can use the functionality of the attributes.
And once you inherit that class again, typically we're not going to be doing something like this,
overwriting it.
So instead, typically what you're going to see is maybe we add some more methods to it and we have
access to those methods within the child class.
All right.
That's it for this lecture.
I'll see you at the next one.