Python Object Oriented Programming in 10 minutes ๐Ÿ

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on everybody it's bro 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 p-o-o-p 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 the 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 tef 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 200 scores 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 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.ear 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 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 name for it let's call this first car just car one 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 all right now let's actually access some of these cars attributes and i should probably put this one in a print statement so print car ones 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 car one i would like this car to use its drive method this car is driving and let's have this car use its stop method car one dot stop this car is 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 in 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 itself 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 car 2 and this will be a different kind of car what kinds of arguments should we pass it let's say this is a ford mustang the year will be 2022 and the color will be red all right let's check car to use attributes and let's have car 2 use its drive and stop method this is a forward 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 dot model plus is driving now this self keyword think of it as you're replacing self with the name of the object that we're working on if car one is using its drive method replace itself with car one if this is car two that called this method then replace itself with car 2. think of it that way and let's do the same thing for stop this plus self dot model plus is stopped all right probably don't need these anymore so let's have car one use its drive and stop method this corvette is driving this corvette is stopped let's try this with car two 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 the stop method this corvette is driving and this mustang has 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 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 [Music] you
Info
Channel: Bro Code
Views: 19,480
Rating: 4.9831815 out of 5
Keywords: python, python oop, python object oriented programming, python objects, object oriented programming python, classes python, object oriented programming beginners, object oriented programming beginners tutorial, oop for beginners
Id: q2SGW2VgwAM
Channel Id: undefined
Length: 10min 4sec (604 seconds)
Published: Mon Dec 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.