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 *

Already have an account? Sign In »

2 hours 30 minutes
Video Transcription
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.
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,
we're gonna learn how we can actually construct our own custom classes.
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
a new folder or a new file. We're going to go call classes
demo dot pie
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
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
imagers, strings and then data structures,
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,
but it's useful enough to understand it when we're learning this sort of practical skill set.
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
just by going to our terminal here. We can actually check out and see,
uh, type.
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,
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
against strings in against me
imagers and lists and dictionaries. The idea here again is just that, because they're implemented his classes, they can have
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.
It has attributes and it has methods. And the methods are the behaviors, the functions that are built into a class.
And the attributes are the variables strings, imagers, dictionaries, whatever that are inside of a class.
So we can again weaken in our colonel here. We can see an implementation of this just by doing class
and will do my custom class
and we'll just give it a simple quality will give it the quality of,
my custom list
custom list.
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
we're going to create. You know where we're going to invoke this class assembly we might with a function,
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.
So let's test that out
and you can see here that it created and stored it there. There were no errors. They got thrown up, and we can verify.
There you go. Now, what's really useful here is to understand that we can create
a whole bunch of new instances of this class
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
so C class, too.
has the list as to see class three.
However, when we change
the customer list in C class too
will say Aye,
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.
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.
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.
And we're going to say my custom function.
Look, we're gonna define that as a function the same we would any other time.
And that's just going to print
And again, we're going to
implement this class,
Not what I meant to do There, There we go,
this time with an equal sign.
sorry about that.
Now it's your shot.
C Class one and you can tear that. My cousin function is already inside.
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,
and you can hear that
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.
And we can examine that we're going to redefine this
custom class, too,
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
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
over Really any given attributes method.
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,
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.
So we're gonna go ahead and we're gonna try this again. We're gonna do C Class one
and we should be able to print now.
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.
I am still in the interpreter. Here we go.
Yep, there
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
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
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,
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
that has the double underscores, that means that it's not for external use, so definite.
And then again, we're going to give it its first argument, which is always going to be self.
And then we might set a couple of these attributes,
which we're going to say
and a
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.
We're not gonna say that you were gonna say name and they're gonna say age.
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
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
dot. Name
equals name and self got. Age equals age,
and we can test this out by writing some simple code. We'll say
see instance equals my custom Class one.
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.
So here we're going to say that the name
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.
We're gonna go ahead and put our shebang line in here the way we always want to do with python programs,
and you can see that it ran without doing anything. Which, of course, happens when I forget to put a print statement,
which is gonna be our second internal function deaf. We're gonna say print vowels,
and then we're going to print self dot name
and so dot age
and make sure I've got all my parentheses there. Correct? I sure don't. There we go.
Now we're gonna run this again,
invoking the print statement. Here we go.
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.
But they don't exist in this inside of this print vowels,
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
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.
So now we've created we've actually
we created. We've actually called our print. Let's see if this runs this time
and didn't run because of the gotcha that I have mentioned earlier.
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.
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.
There's some of the Megan cut out just because of time,
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,
and sure enough, it gave us the name and age,
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.
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