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 *
or

Already have an account? Sign In »

Time
2 hours 30 minutes
Difficulty
Intermediate
CEU/CPE
2
Video Transcription
00:00
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?
00:17
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.
00:37
We saw how we can create a custom class and hopefully took a little bit of time to do that yourself
00:41
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.
01:00
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,
01:03
which I'm not going to rename because this is a terrible name. What is? Obviously
01:07
a person cloths?
01:11
I got a name in an age.
01:14
So you got person here
01:15
and we're gonna get rid of these two lines of junk.
01:19
Now, we're gonna create a second class, and this class is going to inherit from person for a new class. Employees.
01:29
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.
01:48
So when we create our innit function here death,
01:52
innit? So name age department.
01:59
The first thing we actually want to do is call person dot in it.
02:04
Oh, miss those
02:07
and past the two, the two variables that we know relevant along name and age.
02:13
And then we're going to do self dot Department equals department.
02:21
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.
02:31
We'll just call this E m. P equals employees,
02:37
and we're going to give the important arguments we have here Name, age and department.
02:45
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
02:54
works with the interpreter. So, employees,
02:58
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
03:08
25 department is going to be
03:12
research.
03:17
And then again, we're going to do e m p
03:21
dot
03:22
print pals
03:24
and let's see if this works
03:30
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,
03:38
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,
03:59
or
04:00
you can do it
04:02
by creating
04:03
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
04:21
is because in a lot of cases, depending on how complex your class structures are,
04:26
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.
04:43
I tend to avoid that just to create more easily readable and easily interpreted code.
04:47
So the first thing we're gonna do is we're going to print.
04:53
You're really the only thing we're gonna do is go in the front name,
04:58
age
05:00
department,
05:05
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
05:21
and you saw that is worth noting. I'm still calling the parent function that still exists because we didn't overwrite it.
05:29
We could do extended
05:31
print about now if I had overwritten it with just by writing print vows here and then called the parent
05:38
instance by using person dot print values the way I'd within it up here.
05:42
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,
05:54
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.
06:10
And really, the purpose of glasses and python is to represent abstract data types, data concepts.
06:15
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.
06:32
Our lab for lesson one or for module one rather module to there we go got the right module
06:39
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