YTread Logo
YTread Logo

Python Object Oriented Programming (OOP) - For Beginners

Jun 02, 2021
Hello everyone and welcome back, in this video you will learn about

object

-

oriented

programming

in Python and how you can create your own custom

object

s by creating different classes in Python. Now this video is aimed at

beginners

, so people who have some knowledge of Python have written Python code before, but I haven't yet advanced to a sort of intermediate stage which I'll call working with classes and objects and I'm looking to learn that , so I will restart completely from the beginning if I have no knowledge of object-

oriented

programming

, that is totally fine in this series it is for you, if you are someone who just wants a refresher, I would recommend watching the video at two speeds and slowing down when things get a little more confusing. or one that you know you really need to learn more about that topic, so let's go ahead and get started, and what we need to do to discuss object-oriented programming is to first find out what an object is.
python object oriented programming oop   for beginners
Now many people may think that when we talk. about object-oriented programming, that's something completely new that you know you've never seen an empath before and it's a new type of thing to approach and learn when you actually see objects all the time when you're working with Python Code and you just don't know which are objects just because they're a little bit disguised and I'm going to remove that disguise and show you what all these different objects are so that we can have heard of the notion of type before we see this type of function and we know that when we ask what type it is a certain variable or we ask it what types it says: you know a string is like this, it spits out an answer at us and I'll show you what I mean by this if I print the type of hello so we know that hello is a string data type, let's see what we get in our console down here.
python object oriented programming oop   for beginners

More Interesting Facts About,

python object oriented programming oop for beginners...

Oops, I didn't mean to make us get a class string, so notice that this says class now most people just ignore the fact that it says this, but it's actually very important what this tells us is this right here, This string we wrote is actually a string object of class STR just now, what do you think is going to happen? happens when I put in are actually a part of a class and what that means is that, although it doesn't look like we've created an object, when we do something like x equals 1, we've said that X is equal to the object, which is an integer type with the value 1. that is exactly what we have done and the fact that we act by creating these types of objects is very important when writing Python code and I know that this should hopefully not confuse you too much, but if I write, let's say a function to that they know how to define. hello and here we just print hello like this and what I decide to do is go, well, let's look at the type of hello, so notice that I didn't call the function hello with the two square brackets, I'm just looking at the actual name. from hello and we run this okay if we add the trailing bracket at the end here we get the class function so pretty much everything in Python that we work with is actually an object of some kind of class and later in the video we'll see what we will see.
python object oriented programming oop   for beginners
What we are going to do is create our own classes so that we have our own specific types, so these are the so-called built-in types, they are built into the Python language and that is why they work a little differently than other classes that we will work with . More later, but understand that whenever you create something in Python, you are actually creating an object that is an instance of a specific class. That class defines the way that object can interact with other things in our program and I'm just showing you what I mean by that, let's look at some common error messages and we'll see exactly what that means, so we'll say x is equal to 1 and it's equal to hello and we'll see what happens when I try to do something like print which says that because our object x is an INT and our object Y is a string, we can't add them because the program doesn't know how to work. with objects of those two types or rather the addition operation is not defined for int objects and string objects when adding, so it is very important and practically the type we have defines what we can do with the specific variable or with that specific object and denotes the actions that we can perform, you know, plus minus all that, if I were to change this right to 2 and then we just do X plus y, no need to print it, we can Look, that's totally fine and there's no problem because they are both ins which means the operation is defined for them so I hope that is clear and makes some sense.
python object oriented programming oop   for beginners
I hope that gives you an idea of ​​what I mean by objects and now I'm going to talk about methods because they are something that we can do on objects, so I'm sure many of you have seen this before. I'm going to type string equals hello like this and many of you know that we can do something like print a string if I can type this correctly, dots up, so when I type this dot up here, what actually works well? Let's look here, obviously we know that it puts everything in uppercase, but we can use this thing we call method. and this is a method, whenever you have this dot operator, you have some name and then you have the two square brackets at the end here and maybe there are some arguments that go inside there, which are usually a method that acts on a specific object and in this case what it's doing is we have the top method acting on the string object that's stored in this string variable and the reason we can use this method is because it's a string.
Now notice that I can't do something like know that X is equal to one and then do , so these methods, these different operations, the things What we can do with these objects is based on the type of class they are, so that's something we really need to nail down and I hope that makes sense. Now what I'm going to show you is how we can create our own objects. own classes because hopefully that gave you a good enough explanation of what the objects actually are, so to create our own class, I'm going to make a sort of template here and then we'll come back and discuss what it's all doing at once, but just try to follow for now, so I'll say class and we'll just do the classic animal example to start here, so I'll say class dog and we'll define this initialization we'll talk about that later and actually no, let's not even do that, let's just say define the bark that way and then what I'm going to do here is just print the job okay so what I actually did here and I know this is going to be strange to many of you who haven't seen this before I created a class called duck.
What this means is that now I'm going to make my own blueprint type for any object that is of type dog and I'm going to start defining the operations that a dog is capable of doing now in this case I've created a method now a method is essentially just a function that goes inside a class which is the easiest way to define it and for our basic example now all of our methods will start with a parameter called self and we're going to talk about what this self parameter means and how this all works later, but let's move forward to that, so I'm going to say that D is equal to a dog down here and what I What I did when I did this here is I'm actually saying okay.
I'm going to have the variable D and I'm going to assign it to an instance of the dog class, so this is the dog class again, you don't need to know. How this works now, we'll talk about that in a second, but when I type this line here and put dog, which is the name of my class, and then two square brackets like that, I'm instantiating right, creating a new instance of the class . dog, so D will now be an object of type dog and notice that we don't get any errors there and when it decides to print the type of D we will see that we get class underscore, underscore, leading underscore, R squared. dog now the reason we have these underscores is because this tells us which module this class was defined in.
Now, by default, the module that runs is called the main module, so we have these two underscores, main underscore, underscore, but we can see that this is again an object of the dog class and that means that anything that let's define inside here it will be what is allowed or the operations that a dog can perform and one of those operations is barking so this is a method remember I have talked before with a method like the top point so if I want to use that method in an instance of my dog ​​class because this is an instance of the dog class, so what I can do is write well, not top but like dot bark. that, so I can call this method on my dog ​​object again because it's a dog class and I've defined a method that works for dogs, so when I do that we can see that we bark and then obviously it continues to print the type of that object, so this is what we're pulling out here, this is how classes work, we name the class, normally the convention is to use a capital letter and usually you use mixed case, so you like the dog like you say we.
I'm going to make two words that you like, dog, hello, right, so the camel case is like this and then inside the method or inside the method inside the class, you can define some different methods and operations that this object can perform, so what i do. What I'm going to do is say define I don't know, let's just say meow I know this is wrong because it's not a dog and we'll talk about self in just a minute, but I can define another method here and instead of printing something, I could say return, let's say yes, like this, so I don't necessarily need to print something.
I can give something back. I can make these methods take arguments, so maybe I have meow and then I put in X and maybe what I do is I return X plus 1 or something, so maybe let's just call this add, let's call this add underscore 1 , that's going to be the name of our method so we can return Let's take a look at this year and we can see that we get the value 6 so we can create methods that have different associated arguments or parameters like I have put X here and that just means that when I call that method I need to pass a specific value or specific values ​​to that can operate and can work, that's how the methods work, so now let's talk. about itself, but before we can do that, we need to talk about what's called the anit method, which you may have seen me writing early on before I gave up on X.
I didn't want to get too complex so quickly, so now we have two methods, we have this bark method and we have this add one method and I hope that makes sense about how they work and now we're going to talk about this special method here and by the way, this has two underscores so which has an underscore. and then an it and then underline underline now this is a special method and what this allows us to do is instantiate the object right when it's created, so this method will be called every time we type this line, so every time Let's create a new dog.
For example, typing dog and then the two square brackets allow me to get rid of this down here, for now this method will be called and it will pass whatever arguments we put in here, so maybe put something like Tim here and it will pass that to this method, so let's say that whenever we create a dog and we want to give it a name immediately, to create a dog the criteria is that you must give it a name. What we would do here is put the name of the parameter. and then here we would pass the name, so what we need to do is what we are passing in this name.
We need to store this in the dog object somewhere. We need to have this stored. We need to be able to access this. This is the cool thing about items and this is where we'll talk about attributes. What we can do if we want to store this name. We can say that the name of the proper point is equal to the name. Now it is important to note that this does not. They don't have to match, but what we've just done here is we've created what's called an attribute of the dog class, which is name, so what this means is essentially that every time we create a new dog object We will give it a name. through this parameter, self simply remains here to denote the object itself, so every time one of these things is called, we will pass a reference to the object that was called so that we can access things for each specific object and we'll talk about that more in a second, but here what I've done is find an attribute called name that is equal to the name that we passed to it, so what I'm going to do is immediately print inside of herethe name so I can see how this works and notice that when I run this now, Tim prints, so even though I didn't explicitly call this as an it method because I wrote this line here, it passed this name that I put here to the name parameter. and then it printed the name here inside Anette and we also defined an attribute called name which is inside this dog class, so what self does is whenever any of these methods are called invisibly, you can't look at it , the actual reference to this dog object is passed so that we can access attributes that are specific to each dog and what I mean by this is that I can create another dog so that maybe it says d2, which is another instance of the dog class, except this time I named him Bill correctly and now we'll do the print statement again, so we'll say print name just to show how this works and here we have Tim. and we get Bill, so we actually have two different dog objects, now one called Tim and one called Bill, and they both store different names inside there, you know, if that's what you want to call it, so let's show how this works attribute so the The point of this attribute is that it is permanently stored for each specific object, so I can go ahead and come down here and print the name of point D after it's defined and I can print the name of point D like this and when we look here we can see that. those names print, so Tim and Bill notice that I removed the print statement up here, so these attributes when we do something like a point itself and then whatever we want we can call whatever we want is equal to some value at that we can refer to. them later and we can reference them from methods within our class, so an example of that is something like defining getname underscore, so the first argument here will always be self, it's the first parameter and the reason for this is because we need to invisibly pass the actual dog object that you know so that we know which dog was accessing when we go to say the name of the dog, so here we will say define get name and all we will do is return to oneself. dot name like this so now instead of saying dot name we can call thought get underscore name which is a correct method and we can do it here also get underscore name and then we see we get Tim and we get to Bill, so that's the basics behind objects now of course I can also create more attributes so I could do something like H so let's say my dogs I want them to have a name so let's say my dogs want them to have a name and an age, well, what I'm going to do now is say okay, so in our initialization for dog we're going to take a name and we're going to take an H, so we're going to define the attributes here. so that the age of the point is equal to any age.
We pass and now every time I make a new dog object I need to pass a name and an age so this Tim is an old dog, here Bill will be a young one and we'll run this and see that I don't have any problems and if I wanted to I can continue Go ahead and define another method here, so get the age and we'll return the age of the point like that and if we change this to get inches and change this down here to get the age as well. What is the problem here? Wow, so I forgot to put here, so notice what happened when I forgot to put myself, so it's actually a decent mistake.
I didn't put it here as a parameter and you can see. I know it's a bit complicated here, it says that to get age you don't need positional arguments, but we are given one, so what does that really mean? Then he says your arguments are needed, which he did when I didn't here, but I was given one. but I didn't give any arguments inside the square brackets, that's because when we call a method this way, the actual dog object is immediately passed to that method as the parameter itself, so we know which dog you're talking about.
Now if I go ahead and add I, we can see that this works fine and we get thirty-four and twelve, so it's important to remember that now what we can do is create other methods that modify these attributes or create new attributes, so I can do something like, for example, set the age, so in the view I define that the set age will give itself, of course, and then we'll put an age here. What I can do now is say personal thought age is equal to H and now what I'll do for Tim and we'll get rid of Bill for now because I think we understand how it works for different objects, I'm not going to say D dot set age of underline and I'm going to change its age to 23 and when we print it we can see that 23 is actually printed so we can modify it and we can access these different attributes from methods within our class and this is where things get very powerful because this it allows us to access data that is stored inside a specific object and do different things with it depending on how different methods are called correctly and different things and this is more or less the model that defines how a dog actually works, how it operates, what can do, the methods associated with this and the attributes that exist now some of you may be saying, well, why do I need to do this right?
It seems a bit redundant. I could write in a different style. The nice thing about object-oriented programming is that once you create one of these classes you can have an infinite number of instances of this class without having to change anything, so let's say, for example, we're going to leave that class here right now, but we want to simulate what we just did here for the dog Tim. right, we want to have an age and we want to have a name and we want to be able to change your age and all that, well, a lot of you who were beginner programmers would probably tell me we can do something like this, we can say dog. an underscore name equals Tim, you could say dog, an underscore age equals 34 and that's it, you just defined these two attributes, if you want to get them you can access the variable, if you want to change it you can change the variable .
Okay, but what happens when I want to create 25,000 dogs or every time I run my code I want to create a different number of dogs? You can't find a way to write all these different variables that have one, two, three, all the way up to 50,000 or however many dogs you have to represent, that's why we use objects, whenever we are going to reuse something, there are some cases in which we create a class that we are only going to use once. or instantiate once, but those are more complex examples that we won't get into here, but okay, some of you might tell me okay, Tim, I can only make lists.
I can say dogs equals that, we say dogs underline the name equals that. Okay, and here we can put in well, the first name is Tim and then we fill it in, so this will handle the idea that we can't just have all these different variables so that the dogs are the same age and then say I don't know. 32 14 Okay, that's great, but the problem with this is that it's really annoying when I want to access the age of the dog, the name of the dog, and then what if I had 25 other attributes or methods associated with the dog?
This is just a painting. I don't want to deal with that because now I have to find the index of whatever dog I want in a list, which is a time-consuming computer operation, and then I need to reference that index and all the other lists for all the other attributes and It gets very complicated very quickly and let's say I want to delete an instance like feed, you know, the dog object, bill or something like that, at that point I need to find the index of this, I need to find the index of all the attributes and all the other lists and I need to delete them at the same time to make sure that all my data stays consistent and that there is no offset or too many attributes in a list, so it is very complicated to do it like that.
I hope this makes sense as to why we would use an object-oriented style and now what we'll do is go into an example where we create a more complex object and show the advantage of that. Well, many people will usually follow the previous example that I just gave you now. I want to go a little further and show you the advantage of making multiple classes, so instead of using just one class, I want to show you how different classes can interact. In an example where I have a group of students, all of these students have some grade assigned and they are all part of a course and then that course will have some methods to do things like find the maximum grade of all the students. give us the average score of all the students tell us the lowest score some things you might want to do say if you were trying to model or create a system for you know a school or something so obviously this will be a little bit more of an example instead super handy, but I hope this gives you more information on how we would do something like this, so I'll start by cleaning up the creation of a class called student.
I'm going to go a little bit faster here, but I'll slow down and talk about what I did next, so don't worry if you can't keep up, so we'll have one student and each student will have a name, age, and eighth grade, so we're just going to say self name equals name self dot age equals age and then self dot grade equals grade. Now this numerical score will be between 0 and 100, so I'll write it here so we know that and that. should be good for our students so of course we could go in here and add some methods if we wanted something like get a grade of underline and what does ID 1 mean so we have it here and what we can do is return a grade of self duck like Okay, so this is where we're going to leave our student.
Now we could add many more things to this. We could add a rating change. Could you add a test? We can have weightings. We can do all kinds of crazy things. but for now we will leave it at that, so again we have defined three attributes here we have a name, an age and a grade, which are the same as the name, age and grade that we passed when we initially created a student and then we have a method, remember that this is a method and simply returns a student's grade. Now I'm going to create a new class called course.
Now what I'm going to do in this course class is I'm going to have the ability to add students. to a course, so when we create a new course, what we are going to have to do is define the name of the course and the maximum number of underline students that can enroll, so here we will say that the name of the own point is equal to the name and self dot max understudents equals max understudents like this, okay, so what we're going to do now is add a method that will allow us to add students to this course, but how are we going to do that?
I'm going to have students stored inside a course object. Well, what we can really do is make a list of students, so I'll say that students thinking for themselves is equivalent to a blank list. Now notice that I made an attribute and I didn't do it. assign it directly to one of these things that was passed to one of these parameter arguments whatever you want to call them, that's totally fine, in fact, a lot of times it will create attributes like self dot, you know it's active, something like that equals false Okay, we can do that, that's totally fine and the attributes are whatever we decide to define and if we want to assign them to say the argument or parameter that is here, that's fine and we can do that, so here we go, we have students who doubt themselves, we do it. which is a blank list and I'm going to start by creating a method here that will allow us to add a student object to this list, so I'm going to say define add underscore student like this and here antic self and we're actually let's take student, now this student here is actually going to be an instance of a student object and I'll show you what I mean by that in a second, but all we're going to do is say if the length of the students own point is less than and in this case the self thought underline maximum students so what we're going to do is we're going to say self point students dot add student so we're going to create a list of students inside of this you know this list right here that's what we're going to do and we're going to add them only if it's less than the maximum of students in the class and what I'll do is return true if the student was added successfully and then down here.
Otherwise, I'll return false so we can know, maybe, if we create a program next, whether that student was added correctly or not. Now I'll add another method and here I'll say define but average, great wolf code. that in a second, but let's do an example of how now we can add students to this class, so I'm going to make it a little bit smaller so we can read it, so let's do some different students, let's say s 1 equals student What do we need to approve student grades andnamed agent? Let's call that Tim, let's call it 19 and let's call it 95.
Tim is a smart guy, okay and then we'll do s2 equals student, we'll say bill, we'll put it he's also 19 and he's not that smart, he's 75 and then let's do s3 equals and let's go Jill continues the trend Tim billed Jill 19 and maybe she's 65 ok so we've created three students here and these students are right they should work fine let's run this let's make sure everything is fine now , how can we add them to our course? Well, the first thing we need to do is create our course, so we'll say course equals course like this, what do we need? when we create a course we need a name and we need a maximum number of students so let's name this course let's say science or something like that and let's say the maximum number of students will actually be 2 because I want to show what happens when we add a student further of the maximum now we are going to add students, so how do we add them?
We have to call that method for the course point to add students, so here we go, we can add the student s1 and then let's do the course point to add student. again and let's add s2, so now let's run this, we see that everything works fine and let's actually do something that can print something about our student or can show it to our students, so what I could do is say, let's print here. course points and these students and let's take a look here and we get the main student object at some location gibberish main student object at some location gibberish, so what this really tells us is both things inside our list right now our student object and note that what I can actually do is say I index element zero in that list so that the first student we add and decides to call it by the point name, then hopefully it should print. the name of that first student which is 10, that's great and that's how we do it right, we can add things to this course, so now this course stores all of our students and since all of these students have a grade inside our Of course, we'll be able to access that, so now that we've done that, let's write this method that can get the average grade of all the students that are enrolled in the course to do what we're going to need. is to take all the students from the student list we're going to need to add that to an average and then we're going to need to divide that value to find out what it is so what we're going to do is say the value is equal to 0 we're going to say for student in this case auto dot students then we're going to say value plus equals student duck gets a grade like this now notice we could just write great if we wanted to, but I generally like to use the method because let's say we change any attributes later, then this code doesn't will break as long as this method still has the same name and we can modify the method here so that they return the appropriate grade, like let's say we have a student enrolled in multiple courses, then we could determine their grade in a different way and when we call getgrade , maybe we don't just return the self-grade, we return something different, so this continues to work, so we'll say plus value equals student get. grade and now all it will do is simply return, in this case, the value divided by V Len of the students with own points, so let's see what the average grade is for our courses, so that the course gets the grade of the fabric will actually print it like this and run it and we can see that the average rating is actually 85 and that makes sense with the ratings that we have here so that's the idea behind what I'm trying to show you is that we can have different classes, we can have attributes with them and when We can program an object-oriented style now, it doesn't matter how many students we have or how many different courses we have and what students are enrolled in, in fact, what we could do is create another course, we can add the same. students to it and then obviously we would have to change the way their grade was calculated, but that's something we could do now, let's see what happens when I decide to try to add that third student so the course starts adding students like that and we decided to add to student s3, we can see that we get the false value and notice that the average grade doesn't change because we don't actually add that to the course, so we finish the basics about classes and objects and how to create our own classes and hopefully the last example helped you really understand the advantage of doing this.
Now what we're going to talk about here is something called inheritance. This is where we slowly start to get a little more complex and some concepts more difficult, so try to follow them, but I don't find inheritance to be extremely difficult, so the idea behind inheritance and we will show what it is in a second is that you have two classes that are very similar, so let's say we have a general class called maybe okay, let's do a better example, let's say we have a class called dog and a class called cat and let's actually code them to so we can actually see this, say in the cat's init method. what we're going to do is have the proper name and the age like this, so we'll say you know the proper point name is equal if I can type correctly, which apparently isn't happening right now and then the proper point age and then we'll define talk and all we'll do here is just print, you know what this cat is, yeah, okay, and then let's say we have a dog, so we'll say class dog like this and we'll define underscore, underscore, network. in our waste square put the self put the name put the age because that's all we want for that and we'll say self dot name equals name self dot age equals age and then we'll define here talk and the only difference between these two classes is actually the fact that this prints bark instead of meow, so notice that these two classes are almost identical; in fact, there is only one line of code that is different besides the class definition at the top, so there must be a way that we don't need.
By writing this twice, we can use what is called inheritance so that these dog and cat classes can inherit from a higher level class, which means that all that functionality is defined in one place and we only need to write what it is different from those two. classes within them, so ideally what I would like to have is to be able to remove this init class from both and just have the talk method because the only thing that is specific to a cat in a dog, at least for my example, is the fact that one of them says meow and one of them says bark, so let's see how we can do it, by removing what you know was an it method and just having these methods here, what we need to do is create a top level class. which I'm actually going to call pet, so I'm going to say class pet, what I'm going to do here is define an it method that we had before.
Actually, I'm going to define a, let's say, show, so this show method is just I'm going to show me all the things about my objects, so I'm going to say print and here we're going to say, you know, I'm actually going to use a string F. You can You may not know what it is, but don't worry. I am a self-taught name and I am years old. Okay, so what I did was I defined this pet class and this pet class will essentially contain the functionality that I want the cat class and the dog class to have. have and then in the cat class sign and inside the dog class what I will do is define the methods or the attributes or whatever I need to do and that will be different for this specific class, so keep in mind that pet is general, we call this generalization, while cat and dog are more specific, so how can I allow the cat class in the dog class to use this functionality?
Well, what I can do is just add brackets and type pet now, what does this mean? because I am inheriting the top level class pet, then we say this is the general class. This is a more specific class that is being created and inherits from pet and the same with dog. Let me show you how it works, so let's create an instance first of all of the pet class and then we'll create one of the cat and dog class and I'll show you how it works, so let's say P equals pet. Please note that for pet we need a name and we need an age. so let's say it's him, let's put it at 19 and then let's do P dot Show, so let's see this year.
I'm Tim and I'm 19 years old, this is how pet class works. It's pretty simple. Now let's do a cat class. say C, let's say C equals cat. I'm going to say that the cat's name will be Bill and that cat will be 34 years old like this and then let's do the same thing here in a nutshell, now notice that even though there is no method called show inside cat it still appears and says I'm Bill and I'm 34 years old , that's because it inherits the properties of the pack class because I've defined it here and I notice that even though I didn't define it in a method here it still worked fine, you know, this was fine, we initialized it because we used the anit method that was defined inside a pet and of course we can do the same with the dog, so we can say D equals dog, what do we do? call this before Jill 25 and let's do D dot shell so come on I'm in jail and I'm 25 so that works but now let's show what happens when we call to talk about the cat and the dock so if it I decide to call talk like this and we'll call both of them here, you can see we take out and bark and again that's because this talk method is different for the cat class and different for the dog class and since its defined inside here and we instantiate cat when we instantiate cat, well we're going to use the talk method that's defined here and actually what you could do is define talk here and you could say define talk and I could say print, I don't know what I say like this, okay, and then if I decide to change this from show to talk, notice that we're calling talk three times talk is defined here and it's defined in our two child classes is what we call them like that when this is the Top level class, the most general version, any class that inherits from it is known as child classes or derived classes, um, that's not so important to know, but you already know it through that jargon. there and I notice that when I run this we get I don't know what to say, meow and bark, so if there is a method defined in the lower level class or in the child class that has the same name as the top level class, it will do that. will automatically override that method so that it takes care of everything defined here is more specific to this class so it will use that instead of using this top right and you might wonder why we would bother defining one here if it were just I'll get to that later, well we could create another pet, maybe like a fish or something, for example, I can do something like this kind of fish pet like this, I can literally define that and put a pass inside here and now, what I can do.
I mean, okay, let's say F equals fish and then we need a name, so let's call this bubbles, why not give it ten and now we can say F dot, talk and notice what we get? I don't know what to say for this fish class because there was no speech to find inside here, it used the speech that was defined in the top level or parent class, just the one we inherited, so that's the basis behind inheritance. Now it gets a little more complicated. I'm going to show Let's look at the more complex aspects because let's say I want to add an attribute to my cat, so let's say for cats we also care what color those cats are, what I would have to do to do that is this initialization method right because I want to pass it and when I create a cat, but I don't necessarily want to rewrite all of this, so what I'm going to do and I need to rewrite everything, but you see why we would do that.
Do this in a second. I'm going to say proper name, age, coverage like this and now all I'm going to do, let me say that the color of the proper point is equal to the color, so what some of you can say here now that we've defined the color here. is that what we need to do is take this name and age and just copy and paste it here now that would be a correct answer but I'll tell you why we shouldn't do that so the idea here is that sometimes in the initialization method of our parent, there are other things going on besides just redefining the attributes correctly and in that case it wouldn't be right for us to just leave out the fact that we're not going to call this a parent network, we're just going to define the attributes because that could mean that we miss a very important function that you already know is happening from within this initialization to give you an example like say some web applications maybe this initialization actually calls a database and requests information for a database and configure the object using that, it wouldn't necessarily beright, so for me to, you know, take the attributes that we have here and just redefine them as attributes here.
Actually you would still need to call that initialization to make sure the object is set correctly to ensure that happens when we do inheritance like this we need to define the arguments we need, they are the parameters we need for the parent initialization as well I name the age, but there's a fancy way to call it that instead of knowing rewrite this here. In fact, I can call this explicitly and set up our object that way, so to do that I'll say I'm super underline square net done underline underline name age now what this says is super means reference, the super class and the super class is actually the pet class or the class that we inherit from here, so that's what super means the class that we have inherited from, then the underscore underscore defines the method that we want to call correctly and then the name and the age are the arguments we're going to pass to that, so name and age notice I don't need to pass the cell, okay, we don't do it ourselves and it's going to call it and what's going to happen is it's going to run.
Whatever is inside this initialization, that will set the name and age of our object, so we will have those properties defined and then we will call self duck, ah, they are the same color or we will execute that line, so now we have the correct cover. and if I go ahead and I want to define show here then I want to change this show method maybe for this cat object then what I can do is say I am myself that name and I am that age and I am me and in this case it is the color of the dot own, so we can change the display method here and now let's go to cat, let's change this to display, let's add a color because we need to add a color now we have to find it in Annette and let's run this. and let's see what the problem is now that we are getting fish that are not defined, oh did I?
I guess I removed fish or got rid of them at some point. I didn't remember, okay, so anyway let's run this and we can see. that we have, I don't know what to say, I'm Bill, I'm 34, I'm brown and then we bark okay, this is how it works for inheritance and this is how we call the super class or The top level of parent class, right, we need to call this initialization method before we go ahead and do anything else, because the parent initialization can be important, it can do other things, it can call another correct method, so we can't just skip what we need. call it explicitly by writing this line now this line has a bit of a complicated syntax you know it's easy to forget but try to remember it super again it references the main class pet and then we have an it and so on so I hope that gives you a idea.
As for how inheritance works now, it's hard to give really good examples of inheritance without getting into more complex and detailed things, so I'll admit to doing that for now, but remember that when you have classes that do something very similar, they have almost everything identical. Except maybe some attributes or some methods, it might be a good idea to what we call generalize and create a parent class that is a general class that defines the functionality and is used in all of its child classes and that is a very common practice. object oriented programming to use inheritance for example a very good example is something like hierarchy of people so let's say you are working in an organization and the example we want to consider is we have managers and we have the employees now the managers have different access than employees, but employees and managers have very similar properties everyone has a name they will have an age they will have an ID they have a date of birth they have many different things that are the same for both well if we tried to model that system and we were To code it and do it, what we would probably do is create a general top-level class called person that defines all the attributes and all the methods that are general to all people. whether they are managers or employees and then we would create two child classes, one for employee and one for manager, and that would define the specific things that the manager can do and that the employee can do and that are different from each other, so that's the idea behind. inheritance, hopefully that makes sense and now let's move on to the next topic, okay, now it's time to talk about static and class methods and class attributes and in fact, we're going to start with class attributes now, previously you would have seen that every time we defined an attribute for one of our objects we used self correctly and inside the class we had self everywhere, self was referring to the instance we were talking about in that context, you know, so this is what let's do it now.
These are class attributes. Now class attributes are attributes that are specific to the class, not to an instance or an object of that class, so I'll do a basic example where I just create a class person and say number of people equals 0 now here I'm going to define the NIT method and say it is found like this and we're just going to say that each person will just have a name, keep it nice and simple, so that name equals name now let's do p1 person, we'll talk about what which I've done in a second here in case anyone is confused and let's say p2 equals person let's make Jill okay so we have this many people and I'm sure a lot of you It's like what the hell is?
Well, this is a class attribute and the reason it's not a normal attribute is because it doesn't use self, so it's not defined inside any method because it doesn't have access to an instance of the class. is defined for the entire class which means this is not specific again for any instance, it won't change from person to person, although we know something except that the name will be different for each instance of the person class, this is no different for each instance. of the person class, in fact, it's the same thing, so what I can show you is that I can go ahead and print, let's say you know p1 dot, the number of people and that gives me the value 0, but what I can also do because this is not specific. to the instance of any class is the correct person, the number of people and the reason I can write personally because again, this is specific to the class, not the instance, so we can access it just by using the name of the class and that actually means that I can change it using the class name as well so that the number of people and points equals 8, and then if I go ahead and say "ok", P 2, the number of people note that we get 8, although I didn't change it explicitly. in P 2 it changed to P 2 because this was class specific and when I reference this, all it says is when I say P 2 number of people, the way Python interprets this is what is the type of P 2, okay, that's the person. you can see it appears here, it says person, then let's say, does it have an attribute called number of people?
I know that this person itself does not have it, does the class have an attribute called number of people? Yes, we show it and since we change that to 8 that's why we get that value and of course if we do this for P 1 as we have shown correctly, we will get the same number, we will get the number of P 1, people will get 8 and if we decide to change this halfway across to the right, so person equals 9, obviously that's going to become 9 now just because we changed it right before we print the next value, so that's the basics, we're behind of class attributes, there are many different uses for them now, in this case what I want to do is have a number of people, so what I was going to do is in here, say person, the number of people plus is equal to 1 so we can keep track of how many people or how many instances we have created of this person class, so now if I decide to print P, you know, person, period, number of people and we will do it after we create the second one person, and we'll see that we get one, two, and this automatically increments it, so it's a basic example of one where you would use a class attribute.
It's not extremely you, but you know it's something that you might want to consider and sometimes you say you want to define a constant, something like maybe gravity or something that will apply to every person and you want it to be a constant value, so you define. that as a class attribute, so if you ever decide to take this class and use it somewhere else, that constant is still defined instead of putting it here, instead of saying gravity up here and making it equal to, you know, 9 .8 negative meters. per second at the top, what I would do is make it an internal constant as a class attribute, so now whenever you want to access the gravity property to meet a person, what you would do is be able to reference it directly to the class of the person. gravity constant instead of a global constant which may not be there if you put this class in a different file and that's the idea behind these classes also is that they are exportable.
I can write a class to a file and I can take it. and move it to another file and hopefully it should continue working assuming it doesn't depend on anything from the old file, so ideally your classes should be as robust as possible, meaning they don't need anything outside of your initial class . definition unless it's going to be another class that maybe you're interacting with like in the example before we had a course and we had a student, but that's the idea behind class attributes. Now let's talk about class methods so class methods are defined a little differently than regular methods. and in fact, I'll show you how they work.
We have a good example set up here, so I'll say define and in this case we'll say number of people like that instead of actually saying ourselves. I'm going to say CLS, so what we're going to do here is return the number of people from CLS and we're going to use what's called a decorator to indicate that this specific method is a class method and to do that we write the class method act directly above the function or, but yeah, I'm assuming the function method is whatever you want to call, so I know this seems a little strange, but the idea behind it is that this method here won't act on behalf of an instance, it won't be specific to an instance. and in fact, you can invoke it in an instance if you want, but that won't really be very effective.
What this should do is call it in the class itself so that it can handle something like what you know, which returns the number of people. which are associated with this class so these are class methods that means they act on the class itself they don't have access to any instance and that's why I wrote CLS here instead of self because there is no object so What he does is simply act. in this class, so for example, let's say we want to add more people to the quantity, then what you could say is that the class method defines add person like this CLS and we would say that CLS dot is the number of people like this plus equals one, so that would be If these are class methods, we denote them with the class method just so we know that they're not referencing, you know they're referencing the class, so let's take a look at how we use that, now Surely we must necessarily print. the number of people, what we can do is go down here and say person dot number of people and I don't know why there are so many square brackets there, geez, but if we look at this, what we would need to print. first, hopefully we should get the value of 2, so let's see okay, so we need to rename this so it's not the same as the attribute because that's going to be confusing, so let's add an underscore in there because I was realizing what the hell it is.
I continue there and we can see that we get 0, oh, that's because I didn't continue adding here, so actually what I'm going to do is and this is going to be a good example, so let's illustrate this here. I wanted to say it too. but I forgot, I forgot, you know, I didn't keep adding this, but what I can do is use the class method that I've defined here to add a person, so I can say person dot add person like that inside my. and what that will do is call the class method on the person of the class and then it will add to the number of people, so now when we execute, we get two, that's how a class method works.
I don't need access to the instance. to call it, although I can use an instance to call it if I want, I can simply reference the actual name of the class and this doesn't access any specific instance, it just accesses these class attributes or anything specific to the class itself, okay , that has big class methods now let's get rid of them and talk about static methods, so sometimes I'm on removing everything you want to create classes that organize functions together, so for example, you know when you say import math like that. and then you get access to all these math functions like math dot abs or math dot square root or whatever you're going to use well, whicheversometimes they end up doing in object-oriented programming, this is quite common when you have a bunch of functions that you would normally just define like add one like this, yeah, I would do something you define, add two like this, well, what you want to do is organize them into a class and the reason why you do it. it's just to keep it a little bit structured you can move all those classes together into another module and continue using them and to do something like this you want to use what's called a static method now let's make a class called math and what am I going to do here is defining some methods or some functions that I would like to be able to use but they are not specific to an instance, so I don't want to have to instantiate this. math class to be able to use these methods, I want to be able to call them at any point and it doesn't matter if I have an instance of the math class or not, I would like to be able to use them, so what I'm going to do is create what is called a static method, now static means not changing correctly, it means staying the same and that's a very good way to describe what these methods do because they don't have access to an instance like the class method. all they do is something, they do something but they don't change anything, that's the idea behind a static method, they don't change anything because they can't, they don't have access to anything, so here's what we're going to do. it's just saying define add five and here I don't even need to put a cell for a CLS because this won't access anything all it will do is act like a function that is defined inside this class and again something I'm sure many of you They are shouting saying what's the point of doing that, why don't I define a function well globally?
It's more of an organizational thing and there are some more specific applications where you would use a static method, so I need to show them to you, so here we'll take a number and all we'll do in this method is return X plus five, so now, if I really want to use this, what I can do is I don't need to say it. like M equals math like this and create an instance that is not necessary. I can just type the class name and the math dot add five let's put five here let's take a look if I got rid of the s and we get the value ten then this is called a static method and we can create as many of these as we want as well as we can create as many class methods as we want, so maybe we add ten right and then we have a bat like this and we can change this method. say add 10 and now if we run this we get 50 so it's a static method notice it doesn't need anything actually what I can do is say define you know PR let's say it will mean print let's make this a @ static method and now let's just print as if I was just showing you that you don't need any attributes, there are arguments and let's just call PR and now you can see that. we run and since I printed the value of that, it doesn't print them, but there we go like this, okay, so they are static methods and class methods and to be honest, that's pretty much all you need to know about classes and objects in At least at a beginner level, now there are some more interesting things we could talk about, but with the idea of ​​keeping this more For

beginners

and so that everyone can understand, do not confuse.
I am a numerical frame not to discuss anything else. but I hope this has really given you a fundamental understanding of how classes and objects work in Python. A little summary here is to remember that everything we work with is an object in some sense, we have functions that are objects, we have strings that are objects, they are integers. objects and what an object does is an instance of some class and that class defines the properties and almost the model for that object, it says okay so if we have a string we can use the method like upper lower if we have an INT we can add integers and a class, the type of an object is very important because it defines the behavior it can exhibit, so it has been classes and objects in Python and an introduction to object-oriented programming.
I hope you liked it. Please make sure to leave a like. These videos are not that easy to make and are definitely time consuming, so I would appreciate it if you subscribe to the channel and of course let me know if you have any questions or if there is anything you would like to do. see in the future

If you have any copyright issue, please Contact