3.1 Implementing Classes Part 1

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
Video Transcription
00:00
Hello, everyone. And welcome back to Intermediate Python here on Cyber Eri on demand. I am still your instructor, Joe Perry. And this is the first lesson of module to where we're gonna be talking all about classes. So here, in less than one, it's about implementing classes. So first, we're gonna understand what the purpose of a class actually is in Python. We're going to see what they are and how they're made.
00:20
We're only about methods and attributes to concepts that I've alluded to in the past, but they were really gonna dig into in this video,
00:26
we're gonna learn how we can actually construct our own custom classes.
00:30
So we're gonna go ahead. We're gonna jump right into our of'em, as we have been for a little while now, working again out of just a standard a bun to 64 bit vm inside of a terminal. And we're gonna open up
00:40
a new folder or a new file. We're going to go call classes
00:44
demo dot pie
00:48
now. Fundamentally. But when we talk about a class, what we're actually talking about is one of the core concepts of object orientation. So you talk. You hear often about how Python is an object oriented language. You hear about how
01:00
it's built on the basic concepts of object orientation. We don't necessarily hear about what those are very often and fundamentally. The easiest way to understand it is to remember back to what we talked about. If you watch the beginner Python videos way back in the very beginning, where I said there were basically three kinds of data type
01:17
imagers, strings and then data structures,
01:21
and I talked about how that's a little bit of an abstraction. But the concept is that data structures are just this combination, this arbitrary combinations of numbers and strings to reform certain operations and to behave in specific ways. Now that again is kind of an abstraction. Ah, computer science expert would yell at you for saying that Do them,
01:38
but it's useful enough to understand it when we're learning this sort of practical skill set.
01:41
So what a data type what a class actually is is just that arbitrary data type. Almost everything you see in Python is actually implemented as a class, and actually, before we even start writing in this file, we can see
01:53
just by going to our terminal here. We can actually check out and see,
01:57
uh, type.
02:05
There you go. You So you could see that both of these not only the type when we checked against string, but the type of type itself. Both of those are classified as classes. It's the class that is labeled type. Ah, And then, of course, you know, when you actually do type against a specific string,
02:21
you could see that it's still of that class. So it's really fascinating, really important. Understand that the way python actually implements everything is that all of the data in Python is actually implemented under the hood as a class, which can actually make a little bit of sense. When you remember back to some of our previous videos where we were using commands like a door against these
02:40
against strings in against me
02:43
imagers and lists and dictionaries. The idea here again is just that, because they're implemented his classes, they can have
02:50
information, functions and, uh, other variables in their name space that I talked, you know, I'm kind of briefly touched on the concept of name spaces. The idea here is that a class is essentially just this huge container for a specific set of behaviors.
03:06
It has attributes and it has methods. And the methods are the behaviors, the functions that are built into a class.
03:10
And the attributes are the variables strings, imagers, dictionaries, whatever that are inside of a class.
03:16
So we can again weaken in our colonel here. We can see an implementation of this just by doing class
03:23
and will do my custom class
03:29
and we'll just give it a simple quality will give it the quality of,
03:32
Ah,
03:34
my custom list
03:38
custom list.
03:46
Now you see here we don't do anything other than just create this class with a single field. This field right here is called an attribute, and actually, it is any function or any variable inside of a class. So we're gonna go ahead. We're gonna try and print this out, and all we're gonna do to do that is we're going to create an instance of a class
04:04
we're going to create. You know where we're going to invoke this class assembly we might with a function,
04:09
and it's there were instead of getting back to return value, we're going to get back an instance of the class we're gonna get back a usable object built by this blueprint.
04:18
So let's test that out
04:27
and you can see here that it created and stored it there. There were no errors. They got thrown up, and we can verify.
04:36
There you go. Now, what's really useful here is to understand that we can create
04:41
a whole bunch of new instances of this class
04:47
and I'll clear this great little bit. And you can see that after we created these instances the class, they all have that initialized data inthe um
04:56
so C class, too.
05:00
Okay,
05:00
has the list as to see class three.
05:04
However, when we change
05:08
the customer list in C class too
05:11
will say Aye,
05:13
the
05:14
see
05:18
while it modifies the list there, it doesn't matter, doesn't modify it in any other instance of this class, which is what we're looking for, what's kind of valuable information here.
05:29
So even though that natives initialize the same way every time we invoke the class, that data can then be changed in the new class, not modifier. The new instance, without modifying any other instances.
05:39
So that's that's an attribute for a class. But what about a method? What if we wanted to be able to call functions inside of classes. Well, the way we would go about doing that is again we would still in So we could still create our custom class, which in this case, we're going to call my custom class, too.
05:57
And we're going to say my custom function.
06:05
Look, we're gonna define that as a function the same we would any other time.
06:15
And that's just going to print
06:19
test.
06:23
And again, we're going to
06:26
implement this class,
06:30
Not what I meant to do There, There we go,
06:36
this time with an equal sign.
06:44
Oh,
06:57
sorry about that.
06:58
Now it's your shot.
07:00
C Class one and you can tear that. My cousin function is already inside.
07:09
Now, this is an interesting error that just cropped up. And this is a really useful heir to understand, because methods work slightly differently from normal functions, where with the normal function, when you created as we did with this method, you see that it takes no arguments. Everything's fine. All methods inside of a class actually take a least one argument,
07:28
and you can hear that
07:30
this function doesn't take position or use. One was given the reason why all of the functions you implement have to take one argument is because there's sort of a silent, secret argument that is always given inside of a class.
07:41
And we can examine that we're going to redefine this
07:44
custom class, too,
07:49
and you have my custom function. And instead of just giving it no argument, we're gonna give it this special keyword function or keyword argument called self
08:01
now. The reason for that is that when you create this instance of a class, when you, when you create an object from a class in Stan she ate in that class. It has a single has a built in attribute called Self, that identifies its own instance. That's how Python knows to reference the specific changes that you make to a given list variable, for example, as we did with our last
08:18
over Really any given attributes method.
08:22
The way that the Python interpreter knows to understand which which object which class instance is being modified is by addressing that specific self variable. So we have to give all of our internal functions that, as it's for as their first argument,
08:37
and that's an important gotcha when you're creating class functions. You'll see it come up all the time. It's a very, very common error.
08:43
So we're gonna go ahead and we're gonna try this again. We're gonna do C Class one
08:54
and we should be able to print now.
09:00
And sure enough, it'll print test. So that's the basics of creating an instance. Sheeting classes. But let's open up this file that we started to create a second ago and do just a little bit more with it.
09:13
I am still in the interpreter. Here we go.
09:16
Yep, there
09:20
classes demo dot pie. So in that in the Interpreter, you'll recall we created to custom classes. So we called them class my custom Class
09:31
one.
09:35
And you do not put parentheses when you're defining class. My custom class one. Now, instead of doing it instead of just doing that sort of trivial creation of a class that he did in the video we're going to do here is we're actually going to additionally create a special function you're always going to want in a class. And that is the innit function. Now you'll notice here
09:56
that has to underscores before into 200 scores. After that, pre pending an app ending is indicating that this is a private function. This is a function that is only for use by the class and should not be addressed outside of it. Now they're pretty easy ways to get around that it's not impossible to address internal functions and python. They're not very heavily protected,
10:15
but it is something worth knowing. It's something to remember that if you see a function when you do your door whenever you're looking at a class
10:20
that has the double underscores, that means that it's not for external use, so definite.
10:26
And then again, we're going to give it its first argument, which is always going to be self.
10:31
And then we might set a couple of these attributes,
10:35
which we're going to say
10:37
name
10:41
and a
10:48
And since we want to set those in this in this function, we obviously need to take them as arguments. We can't make them up out of thin air, so we're going to say name equals.
10:58
We're not gonna say that you were gonna say name and they're gonna say age.
11:03
And so these air now arguments to creating this class, these air arguments when in Stan she ating an object in this class and then inside of it. We just address these attributes that you see up here
11:13
by addressing them according to the self object. So you'll remember when we imported the OS module. Some of the functions we made were still inside that module. We had to address them with this period. Same general concept happens with classes we're going to address with self
11:26
dot. Name
11:28
equals name and self got. Age equals age,
11:35
and we can test this out by writing some simple code. We'll say
11:41
see instance equals my custom Class one.
11:46
Now you'll recall the one we in Stan. She ate it. We created objects earlier. We did it by giving it these parentheses. Now we're gonna do the same thing, but we're going to give it arguments. And those arguments are going to the arguments that get passed on to in it. Now. You don't have to give it the self argument that's automatically given for any classmethod and said you're just going to give it the two arguments for name and age.
12:05
So here we're going to say that the name
12:07
is going to be leave because that's my boss, that makes me laugh. But his name and videos and his age, I have absolutely no idea how old he is. So I'm gonna say 40 because that seems safe. And then we're going to close and we're gonna save before we close and save this. I almost missed it.
12:24
We're gonna go ahead and put our shebang line in here the way we always want to do with python programs,
12:41
and you can see that it ran without doing anything. Which, of course, happens when I forget to put a print statement,
12:50
which is gonna be our second internal function deaf. We're gonna say print vowels,
12:56
and then we're going to print self dot name
13:11
and so dot age
13:16
and make sure I've got all my parentheses there. Correct? I sure don't. There we go.
13:26
Now we're gonna run this again,
13:33
invoking the print statement. Here we go.
13:41
And what I want to address here something that should be kind of interesting to you watching this is that these two variables, this self dot name and the self dot age aren't defined anywhere in sight of this second function they air created appear at the top in the in this class, and then they're actually given values in the in its function.
14:01
But they don't exist in this inside of this print vowels,
14:05
and the reason why we're able to address them directly is because we're able to address them according to that self object, that instance object that we were talking about. So we're able to make use of these in any method in the class, even though there have not been directly defined in that method. It's one of the main uses of class. You're able to keep data stored in that way and access it much more easily
14:24
rather than passing. It is a variable er's and argument all the time. You could just keep it stored in one of those upper level variables or one of those upper level attributes they're able to access from any of your methods.
14:35
So now we've created we've actually
14:39
we created. We've actually called our print. Let's see if this runs this time
14:46
and didn't run because of the gotcha that I have mentioned earlier.
14:50
Where we have inside of this print vows, we did not give it any arguments, so we're gonna go ahead. We're gonna give it a self argument, and this time for sure it will run.
15:01
I mentioned before in videos that I make a habit of trying to leave sort of those coding process in the video. The reason for that, as I've said before, is because this is a trial and error process, so you're gonna run into a lot of cases where something should work. You feel like you've done all the right code, and then you go back and you see that it's a minor and I try to leave those encoding videos wherever possible.
15:20
There's some of the Megan cut out just because of time,
15:22
but that's that's why when you watch these videos, you might see me go back and forth to the file a few times because that's part of the process of being a programmer is isolating your mistakes and fixing them kind of piece by piece. So classes demo run it for the final time,
15:35
and sure enough, it gave us the name and age,
15:39
so it's going to the end of this video. In the next video, we're gonna talk about the concept of inheritance within classes, so we're gonna have another short video on this lesson, and then we're gonna jump on to our next lesson. But for now, we're gonna come back. We're going to talk about the concepts of inheritance and building classes based upon other classes.
15:56
Thank you for watching. I've been your instructor, Joe Perry, and you're watching Intermediate Python here on Cyber Eri on demand.
Up Next