Hey, what's going on everybody? It's Brill. Hope you're doing well. And in this video I'm going to explain object-oriented programming in Python. So sit back, relax, and enjoy the show.
If I could have just a moment of your time, please like, comment, and subscribe. I would greatly appreciate it. Welcome to today's topic on Python object-oriented programming, also known as POOP, and in today's video we're going to be creating objects. An object is an instance of a class.
By using programming we can create representations of real-life objects. So look around you, wherever you're sitting or standing right now. You are surrounded by objects.
Next to me I have a phone, a television, some snacks, and I'm talking into a microphone. Point being, we can use programming to mimic real-world objects by assigning a combination of attributes, what an object is or has, and methods, what an object can do. Now, in order to create an object, we'll need to create a class. A class can function as a blueprint that will describe what attributes and methods that our distinct type of object will have. You can either create your class within your main module, or you could create a separate file dedicated solely for your class.
Now to create a class, we would type class and then the name of the object that we would like to create. So today I would like to create some car objects because I like cars. So we would type car. And a common naming convention with class names is that they should be capital. So car is going to have a capital C.
And for the time being, we need to type in something. I'll just type in pass as a placeholder. So if you have a small program, it may be better to write your class within your main module, but if your class is fairly large, you may want to consider placing your class within a separate module.
So if you were to take that route, we would go to File, New, Python file, and we would name this car, click Python file, and we would declare our class within this separate module. So class car and for now I'll type in pass. Then we just need to import this class so within our main module we would type from the name of the module import the name of the class.
From car import car. Now objects can have some combination of attributes and methods. Attributes describe what an object is or has.
So what are a few attributes that cars might have? They might have a make, so let's create a few variables. For the time being, I'm just going to use none as a placeholder.
Cars can have a model, a year, and a color. Now, objects can also have methods. What kinds of methods could cars perform? Perhaps a drive method and a stop method.
So let's define those. def, let's say drive, and we'll have one argument, self. Self refers to the object that is using this method. Now, what do we want to do when we call this method?
Let's say this car is driving. Let's create a stop method as well. def stop And we will print this car is stopped.
We now have all of the different attributes and methods we would like our car objects to have. But there is one more thing that we need. It is a special method called the init method that will construct objects for us.
In other programming languages, this is known as the constructor. So we need a special method that will create objects for us. So we need to define this method, def. And the syntax on this is somewhat strange.
It is two underscores, I-N-I-T. it's short for initialize, two underscores again, and then we need at least self as an argument, and then we can actually assign our car objects unique variables. So take all of these attributes that we have, and we're going to place them within this init method. Now we can receive arguments when we create car objects, but we need to pass them in as arguments to our init method. So we need to set up some parameters.
Let's say in order to create a car object, we need a make, and we need to set up a make. a model, a year, and a color. Then when we receive these arguments, we can actually assign them to each car's specific attributes, but we need to precede each of these with self. Self is referring to the object that we're currently working on or creating. So self.make, self.model, self.year, and self.color.
And then when we assign these, we're going to say self.make equals whatever make that we receive when it's passed in as an argument, self.model equals model, and continue that pattern for year, and color. All right, that is all we need. need for our class car we have a constructor we're assigning arguments that we receive to the attributes of our car object and we also have two methods one for drive and one for stop now we can start creating some car objects so going back to my main file here to create an object we need a unique you Let's call this first car just car1 equals the name of the class car then a set of parentheses.
Now in order to construct a car object we need to pass in a matching set of arguments. You can see here that we need to pass in a make, a model, a year, and a color. So what kind of car should we make?
Let's say that the make is going to be Chevy. We need a model, perhaps Corvette. a year, 2021 is good, and a color, let's say blue.
Alright, now let's actually access some of these cars attributes, and I should probably put this within a print statement. So print car1s make, and this should print chevy. Then we can do the same thing for model, year, and color.
So let's change some of these around. Model, year, and color. Yep, this object is a Chevy Corvette 2021, and the color is blue. And we should have two methods as well.
So car1, I would like this car to use its drive method. This car is driving. And let's have this car use its stop method. car1.stop This car has stopped. There's one thing that I should mention real quick because I forgot to mention it earlier.
So within our init method, you can see that we need five arguments in order to construct a car object. Self, make, model, year, and color. But when we pass in... our arguments, we're not passing in anything for self. We only have four arguments here.
That's because with Python, we do not need to pass in self. That's done automatically for us. We're referring to the object that we're dealing with. As you can see with our drive, and stop method, we need to pass in self in order to execute our method. But when we call this method, we do not need to do so.
So just remember with Python with self, we do not need to pass in anything for this argument. That is all. Now the nice thing about this is that we can reuse this class as a blueprint to create more car objects, we just call that init method, that constructor. So this time let's create a second car called car2, and this will be a different kind of car.
What kinds of arguments should we pass in? Let's say this is a Ford Mustang. The year will be 2022, and the color will be red. All right, let's check car2's attributes, and let's have car2 use its drive and stop method.
This is a Ford Mustang. The year is 2022. The color is red. This car is driving and this car is stopped.
One other thing that we can do too is that within our methods here, let's replace car with the name of the model that we're working with. So this. plus self.model plus isDriving. Now this self keyword, think of it as you're replacing self with the name of the object that we're working on.
If car1 is using its drive method, replace self with car1. If this is car2 that called this method, then replace self with car2. Think of it that way. And let's do the same thing for stop. This plus self dot model plus is stopped.
Alright, probably don't need these anymore. So let's have car1 use its drive and stop method. This Corvette is driving, this Corvette is stopping.
Let's try this with car 2. This Mustang is driving. This Mustang is stopped. Now you can have them do things independently too. Let's have car 1 use its drive method and car 2 use its stop method. This Corvette is driving and this Mustang is driving.
Mustang is stopped. In conclusion, a class can function as a blueprint to create objects. We can assign attributes, what describe an object is or has, and methods, what each object can do. And then within our class, we have a special method called the init method.
We can pass in some arguments and assign these arguments to each object's attributes, and then we can reuse this class as if it was a blueprint so we can create more objects out of it. So that is it. the basics of object-oriented programming in Python.
If you would like a copy of all this code, I will post this in the comment section down below. But yeah, that's the basics of object-oriented programming in Python. Hey you, yeah I'm talking to you. If you learned something new, then help me help you in three easy steps by smashing that like button, drop a comment down below, and subscribe if you'd like to become a fellow bro.