3.2 Implementing Classes Part 2

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *

Already have an account? Sign In »

Video Transcription
Hello and welcome back to lesson one of module to of intermediate Python here on Cyber Eri on demand I as always, and your instructor, Joe Perry. And if you're watching this, you got through the what? I thought when I was recording it would be one. But what turned into two class videos that came before this and lesson one?
Fortunately, you were almost through the woods. This is gonna be the last video of this lesson, which is definitely gonna be the longest lesson of this course. So in this in the last video, we talked about creating classes from scratch. We created you. We talked about the concept of attributes which are just variables that air contained inside of a class and methods which are functions which belonged to a class.
We saw how we can create a custom class and hopefully took a little bit of time to do that yourself
in this video, have we're gonna learn that you don't have to always create classes from scratch by taking advantage of a core concept of object orientation called inheritance. Inheritance is the idea that you can build a class based upon an older class and not have to do any hard working creating it or at least have to do much less hardware.
So to do this, we're gonna go back to our classes demo here. And you could see that we have my custom class one,
which I'm not going to rename because this is a terrible name. What is? Obviously
a person cloths?
I got a name in an age.
So you got person here
and we're gonna get rid of these two lines of junk.
Now, we're gonna create a second class, and this class is going to inherit from person for a new class. Employees.
I figured since I already named my boss in the last video, I might as well do this here. So class employees, And instead of just giving the colon like we did with person, we're actually going to give this one parentheses. This is how you indicate to the python interpreter that the class employee is going to inherit all of the attributes and methods of the parent class person.
So when we create our innit function here death,
innit? So name age department.
The first thing we actually want to do is call person dot in it.
Oh, miss those
and past the two, the two variables that we know relevant along name and age.
And then we're going to do self dot Department equals department.
So that's all we needed to do to create a completely new type of class that inherits from person. And we can see that in action pretty easily. By typing again, we're going to create.
We'll just call this E m. P equals employees,
and we're going to give the important arguments we have here Name, age and department.
I very nearly missed this, innit? We also want to pass the argument itself, because that is going to be relevant in passing up to your parent class so that it
works with the interpreter. So, employees,
we're going to say name is going to be Joe. I won't name any more co workers, uh, age not gonna tell you my real age. We're gonna call it
25 department is going to be
And then again, we're going to do e m p
print pals
and let's see if this works
Well, we got my name in my age, but we didn't get that third variable that third attribute that we had just gone through the trouble of creating,
and the reason for that is because this print vows were inheriting doesn't know to address that. It's still the same function that we had before, which means that we're going to have to extend that function. We're going to have to re implement it here so that can actually perform what we wanted to perform, and we could do that pretty easily. We can do that by just creating a new print valves,
you can do it
by creating
extended. Now. This isn't actually the only way to implement this. One of the really interesting since the you can do is call the parent function inside of it by by addressing a person dot print vowels. The reason why I tend to create custom functions underneath it
is because in a lot of cases, depending on how complex your class structures are,
it's very easy to create 10 different classes that are inherited from one parent class that all implement some of the values or some of the variable slightly differently. So one of the really cool things about extending is that you can just add to the previous function and then, you know, make use of it through the parent call.
I tend to avoid that just to create more easily readable and easily interpreted code.
So the first thing we're gonna do is we're going to print.
You're really the only thing we're gonna do is go in the front name,
and this Linda code's gonna get a little bit ugly just because of the length or the font size that I'm using here. So the department
and you saw that is worth noting. I'm still calling the parent function that still exists because we didn't overwrite it.
We could do extended
print about now if I had overwritten it with just by writing print vows here and then called the parent
instance by using person dot print values the way I'd within it up here.
Uh, when we call that it would directly have changed, However, because of the fact that we used a new function name, we obviously have to address a new function,
and we can run classes, defeat a pie and see that it has been updated. That's the basics of inheriting from parent classes. That's all there is gonna be in this video that's gonna actually also be the end of lesson one. So in less than one, we talked about what classes are we talked about? Their purpose.
And really, the purpose of glasses and python is to represent abstract data types, data concepts.
We learned about methods and attributes methods. Of course, being functions of belong to a class and attributes being variables. And then we learned how to construct class both from scratch and inheriting from apparent class. So hopefully this was a valuable lesson for you. Hopefully, the last 30 minutes or so of class teaching has been valuable and you understand them a little bit better.
Our lab for lesson one or for module one rather module to there we go got the right module
eyes gonna heavily focus on classes, obviously. So you're gonna get plenty of personal and practical experience on that on I highly recommend you take a little time to mess around with the class assignments in that So thank you for watching. As always, I have been your instructor and you're watching Intermediate Python here on cyber Eri on demand
Up Next
3.3 Iterators and Generators Part 1
3.4 Iterators and Generators Part 2
3.5 Exceptions
3.6 Summary and Review
4.1 Module Deep-Dive