Python Object Oriented Programming (OOP) - For Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome back so in this video you're gonna learn about object-oriented programming in Python and how you can create your own custom objects by making different classes in Python now this video is geared towards beginner so people that have some knowledge of Python have written Python code before but I've not yet stepped up into kind of the intermediate stage which I'm gonna call is this working with classes and objects and are looking to learn that so I'm restarting completely from the beginning if you have no knowledge of object-oriented programming that's totally fine in this series is meant for you if you're someone who just wants a refresher I'd recommend maybe watching the video on two x speed and slowing down when things get a bit more confusing or one you know you need to really learn that topic more so let's go ahead and get started and what we need to do to discuss object-oriented programming is first to find what an object is now a lot of people may think that when we talk about or object-oriented programming that's a completely new thing you know you've never seen an empath on before and it's a new kind of thing to tackle and to learn when in reality you actually see objects all the time when you're working with Python code and you just don't know that they're objects just because they're a little bit disguised and I'm gonna kind of take away that disguise and show you what all these different objects are so we may have heard of the notion of type before we see this function type and we know when we ask it what type a certain variable is or we ask it what types say you know a string is like this it spits out an answer to us and I'll show you what I mean by that so if I do print type of hello so we know that hello is a string datatype let's just see what we get in our console down here oops I didn't mean to do that we get class string so notice that this says class now most people just disregard the fact that it says this but this is actually very important what this is telling us is this right here this string that we typed in is actually an object of the class string right STR now what do you think is gonna happen when I put in X well we know that X is an integer so let's have a look here we can see that we get class int so notice that these different data types that we've called them before a string and an int are actually a part of a class and what that means is even though it doesn't look like we've created an object when we do something like x equals 1 we've said X is equal to the object which is a type integer with the value 1 that's exactly what we've done and the fact that we act create these kind of objects is very important in writing Python code and I know this hopefully shouldn't confuse any of you too much but if I write say a function so you know define hello and in here we just print hello like that and what I decide to do is I go okay let's look at the type of hello so notice I didn't call the function hello with the two brackets I'm just looking at the actual name of hello and we run this well if we add the final bracket at the end here we get class function so pretty much everything in Python that we work with is actually an object of some kind of class and later on in the video what we're gonna do is make our own classes so that we have our own specific types so these are what are called the built-in types these are built into the Python language and that's why they work a little bit differently than other classes that we'll work with later but just understand that whenever you create something in Python you are really creating an object which is an instance of a specific class in that class defines the way in which that object can interact with other things in our program and just show you what I mean by that let's just look at some common error messages and we'll see exactly what that means so we're gonna say x equals 1 y equals hello and watch what happens when I try to do something like print X plus y well we get some air let's go up and see here we get type error unsupported operand types for plus int and string so it's saying because our object x is an INT and our object Y is a string we cannot add them because the program does not know how to work with objects of those two types or in better words the addition operation is not defined for objects of int and objects of string at being added together so it's very important and pretty much the type that we have defines what we're allowed to do with the specific variable or with that specific object and it denotes the actions that we can perform you know plus minus all of that if I were to change this right to 2 and then we just do X plus y don't need to print it out we can see that that's totally fine and there's no issue because these are both ins which means that operation is defined for them ok so hopefully that is clear and that makes some sense I hope that gives you kind of understanding of what I mean by objects and now I'm gonna talk about methods because those are something that we can perform on objects so I'm sure that many of you seen this before I'm just gonna type string equals hello like that and many of you know that we can do something like print string if I can type this correctly dots up right so when I write this dot upper here what is that actually doing well let's look at here we obviously know that puts that all to uppercase but we're allowed to use this what we call method and this is a method whenever you have this a dot operator you have some name and then you have the two brackets at the end here and maybe there's some arguments that go inside of there that is usually a method that is acting on a specific object and in this case what it's doing is we have the method upper acting on the object of type string that is stored in this string variable and the reason we're allowed to use this method on it is because this is a string now notice that I can't do something like you know X equals one and then do X dot up and the reason I can't do that is because while it's gonna say right here int object notice int object has no attribute upper so these methods these different operations the things that we can do with these objects is based on the type of class that they are so that's something that we really need to nail in there and I hope that makes sense and now what I'm gonna show you is how we can actually create our own classes because hopefully that gave you a good enough explanation for what objects really are so to create our own class I'm gonna kind of do like a template here and then we'll go back through and discuss what everything is doing at once but just try to follow along for now so I'm gonna say class and we're just gonna do the classic kind of animal example to start here so I'm gonna say class dog and we're gonna define this initialization which we'll talk about later and actually no let's not even do that let's just say define bark like that and then what I'm gonna do in here is simply print work okay so what I've actually done here and I know this is gonna be weird for many of you that have not seen this before is created a class called duck what this means is I'm gonna make my own kind of blueprint now for any objects that are of type dog and I'm gonna start defining the operations that a dog is able to do now in this case I've created a method now a method is essentially just a function that goes inside of a class that's the easiest way to define it and for our basic example right now all of our methods are gonna start with a parameter called self and we're going to talk about what this self parameter means and how all this works later on but let's kind of build our way up to that so I'm gonna say D equals a dog down here and what I've done when I did this here is I'm actually saying okay I'm gonna have the variable D and I'm going to assign it to a instance of the class dog so this is class dog again you don't need to know how this works right now we're gonna talk about that in a second but when I write this line here and I put dog which is the name of my class and then two brackets like that I am instantiating right creating a new instance of the class dog so D is now gonna be an object that is of type dog and notice that we don't get any errors there and when I decide to print out type of D we're gonna see that we get class underscore underscore main underscore R squared dog now the reason we have these underscores is because this is telling us what module this class was defined in now by default the module that you run is called the main module that's why we have these two underscores main underscore underscore but we can see that this is again an object of the class dog and that means that whatever we define inside of here is going to be what is allowed or the operations that can be performed by a dog and one of those operations is bark so this is a method remember we've talked to a method just like dot upper before so if I want to use that method on an instance of my dog class because this is an instance of the class dog then what I can do is I can type well not upper but dot bark like that so I can call this method on my dog object again because it is a class dog and I've defined a method that works for dogs so when I do that we can see that we get bark and then it still obviously continues to print the type of that object so this is what we're getting out here so these are kind of how classes work we name the class typically the convention is to use an uppercase letter and usually you go camel case so you do like dog like you say we're gonna do two words you do like dog hello right so camel case like that and then inside of the method or inside the method inside of the class you can define a few different methods and operations that can be performed by this object so what I'm gonna do is I'm gonna 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 just say yeah like that so I don't necessarily need to print something I can return something I can have these methods take arguments so maybe I have meow and then I put X and maybe what I do is I return X plus 1 or something like that so maybe let's just call this add call this add underscore 1 that'll be the name of our method so I can return X plus 1 and then what I could do if I want it here is like it's a dog add underscore 1 put 5 there and let's print out the value that we get let's have a look at this year and we can see that we get the value 6 so we can make methods that have different arguments associated with them or parameters like I've put X here and that just means that when I call that method I need to pass in a specific value or specific values so that that can operate and that can work so that is how the methods work so now we're gonna 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 typing at the beginning before I kind of gave up on X I didn't want to get that complex that fast so right now we have two methods we have this bark method and we have this add one method and I hope those makes sense on how those work and now we're gonna talk about this special method here and by the way this has two underscores so it has underscore underscore and then an it and then underscore underscore now this is a special method and what this pretty much allows us to do is instantiate the object right when it is created so this method will be called whenever we write this line so whenever we create a new dog instance by writing dog and then the two brackets let me get rid of this down here for right now this method will be called and it will pass any arguments that we put in here so maybe I put something like Tim in here it will pass that to this method so let's say that every time we create a dog and we want to give it a name immediately so to create a dog the criteria is that you must give it a name well what we would do in here is we would put the parameter name and then here we would pass in the name so what we need to do though is what we're passing in this name we need to store this in the dog object somewhere right we need to have this stored we need to be able to access this this is kind of the nice thing about objects and this is where we're gonna talk about attributes what we can do if we want to store this name is we can say self dot name equals name now it's important to note that this doesn't these don't need to match but what we've just done here is we've created what's called an attribute of the class dog which is name so what this means is essentially every time we create a new dog object we will pass a name through this parameter so self just stays here to denote the object itself so like every time one of these things is called we will pass a reference to which object it was called on so 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 to find an attribute called name which is equal to the name that we passed in so what I'll do is immediately I'll just print inside of here the name so that we can see how this works and notice that when I run this now we get Tim printing out so even though I didn't explicitly call this an it method because I wrote this line here it passed this name that I put in here to the name parameter and then it printed out name here inside of the Anette and we also defined an attribute called name that is inside of this dog class so what self is doing is every time that any of these methods is called kind of invisibly you don't get to see 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 that is I can make another dog so maybe I say d2 which is you know another instance of the class dog except this time I named it's Bill right and now we'll do the print statement again so we'll say print name just to show how this works and here we get Tim and we get Bill so we actually have two different dog objects now one named Tim and one named Bill and they both store different names inside of there you know instance if that's what you want to call it so let's just show how this attribute works so the point of this attribute is that it's stored permanently for each specific object so I can actually go ahead and go down here and print D dot name after that's defined and I can print D - dot name like that and when we look here we can see that those names get printed out so Tim and Bill notice that I remove the print statement from up here so these attributes when we do something like a self dot and then anything that we want we can call whatever we want is equal to some value we can reference them later on and we can reference them from methods inside of our class so an example of that is something like say define get underscore name so the first argument here is always gonna be self are the first parameter and the reason for that is because we need to invisibly pass the actual you know dog object itself so that we know which dog were accessing when we're gonna get say the name of the dog so here we're gonna say define get name and all we're gonna do is return self dot name like that so now rather than saying dot name we can call thought get underscore name which is a method right and we can do that down here as well get underscore name and then we see that we get Tim and we get Bill so that is the basics behind objects now of course I can make more attributes as well 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'll do now is I'll say okay so in our initialization for dog we're going to take a name and we're gonna take an H so we'll define the attributes in here so self dot age is equal to whatever age we passed in and now every time that I make a new dog object I need to pass a name and an age so these this Tim will be an old dog here bill will be a young one and we'll run this and we see that we get no issue and if I wanted to I can go ahead and define another method here so get age and we'll return self dot age like that and if we change this to get inch and we change this one down here to get age as well what is the issue here oops so I'm forgot to put myself in here so notice what happened when I forgot to put myself so that's actually a decent error to run into I didn't put self in here as a parameter and you can see I know that it's kind of messy here it says get age takes zero positional arguments but one we're given so what does that actually mean so it's saying it takes is your arguments which it did when I didn't himself here but one was given but I didn't give any arguments inside of the brackets well that's because when we call a method this right the actual dog object itself immediately kind of been Blee gets passed to that method as the self parameter so we know which dog it's talking about so now if I go ahead and I add self we can see that this works fine and we get thirty-four and we get twelve so that's important thing to remember now what we can actually do is we can create other methods that modify these attributes or create new attributes so I can do something like for example set age so I in sight define set age will give itself of course and then we'll put an age here what I can do now is say self thought age is equal to H and now what I'm gonna do for Tim and we'll get rid of Bill for now because I think we get the point on how it works for different objects isn't gonna say D dot set underscore age and I'm gonna change his age to 23 and when we print it out we can see that 23 actually gets printed out so we can modify and we can access these different attributes from methods inside our class and this is where thing gets things get very powerful because this allows us to access data that is stored within a specific object and do different things with it based on how different methods and different things are being called right and this just is pretty much the blueprint that defines how a dog actually works how it operates what it can do the methods associated with it and the attributes that exist now some of you may be saying well why do I need to do this right like this seems kind of redundant I could write in a different style well the nice thing about object-oriented programming is once you create one of these classes you can have an infinite amount of instances of this class without having to change anything so let's say for example that we'll leave that class up here right now but we want to simulate what we've just done here for the Tim dog right we want to have an age and we want to have a name and we want to be able to change his age and all that well many of you that were more beginner programmers would probably tell me we can do something like this we can say dog one underscore name equals Tim you could say dog one underscore age equals you know 34 and there you go you just defined these two attributes if you want to get it you can just access the variable if you want to change it you can just change the variable okay nice but what happens when I want to make 25,000 dogs or every time I run my code I want to make a different amount of dogs you can't find a way that you can write all these different variables that have one two three all the way up to 50,000 or however many dogs I have to represent that that's why we use objects is whenever we're going to be kind of reusing something there is some instances in which we make a class where that we're only going to use once or instantiate once but those are kind of more complex examples we won't get into here but then okay so some of you might tell me all right well Tim I can just do lists I can say dogs equals that we say dogs underscore name equals that right and here we can put well the first name is Tim and then we had fill so this will handle the idea that we can't just have all these different variables right so dogs age equals and then we go let's say I don't know 32 14 okay that's great but the issue with this is that it's really a pain when I want to access the dog's age the dog's name and then what if I had like 25 other attributes or methods associated with the dog this is just just a paint you don't want to deal with that because I have to now find the index of whatever dog I want in one list which is a very time-consuming operation in computing and then I need to reference that index and all the other lists for all the other attributes and it just becomes very messy very quickly and let's say I want to delete one instance like feed you know the dog object bill or something like that right then I need to find the index of this I need to find the index of all of 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 there's no kind of offsetting things or too many attributes in one list so it's just as a pain to do it like that so I hope this makes sense on why we would do object-oriented style and now what we'll do is go into one example where we create a more complex object and show kind of the advantage of that ok so a lot of people typically will stomp at kind of the previous example that I just gave you now I want to go a little bit further and show you the advantage of doing multiple classes so rather than just using one class I want to show you how different classes can kind of interact in an example where I have a bunch of students these students all have some grade assigned to them and they're all a part of a course and then that course will have some methods on it to do things like find the maximum grade of all students give us the average grade of all students tell us the lowest grade some things that you may actually want to do say if you were trying to model or create a system for you know a school or something like that right so this is obviously going to be a little bit more of an example as opposed to super practical but hopefully this should give you some more insight into how we would do something like that so I'm going to start by cleaning creating a class called student I'm gonna go a little bit faster here but I will slow down and talk about what I've done after so don't worry if you can't keep up so we're gonna have a student and each student is gonna have a name age and eighth grade so we're just gonna say self the name equals name self dot age equals age and then self dot grade equals grade now this numeric grade is gonna be between 0 and 100 so I'll just write that down here just so we know that and that should be good for our students so we could of course go in here and add some methods if we wanted something like get underscore grade and what does ID 1 just so we have it in here and what we can do is return a self duck grade like that okay so this is this is where we're gonna leave our student now we could add a lot more things to this we could add a change grade we could you know add a test we can have weightings we can do all kinds of crazy stuff but for now we're gonna leave it at that so again we've defined three attributes here we have a name and age and a grade those are equal to the name age and grade that we pass in when we initially create a student and then we have one method remember this is a method and it simply returns a student's grade now I'm going to make a new class called course now what I'm gonna do in this course class is I'm gonna have the ability to add students to a course so when we create a new course what we're gonna need to do is we need to define the name of the course and the max underscore students that can be enrolled so in here we'll say self dot name equals name and self dot max underscore students equals max underscore students like that all right so what we're gonna do now is we're gonna add a method that will allow us to actually add students to this course but how are we gonna do that how are we gonna have students stored inside of a course object well what we can actually do is we can make a list of students so I'm gonna say self thought students equals a blank list now notice that I've made an attribute and I didn't assign it directly to one of these things that was passed into one of these parameters arguments whatever you'd like to call them that is totally fine in fact a lot of times will create attributes like self dot you know is active something like that equals false right we can do that that's totally fine and attributes are just whatever we decide to define and if we want to assign them to say the argument or parameter that's here that's fine and we can do that so there we go we have self-doubt students which is a blank list and I'm gonna start by creating a method here that's going to allow us to add a student object into this list so I'm going to say define add underscore student like that and in here antic self and we're actually going to take student now this student right 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 gonna do is say if the length of self dot students is less than and in this case self thought max underscore students then what we'll do is we'll say self dot students dot append student so we're gonna create a list of students inside of this you know this list right here right that's what we're gonna do and we're gonna add them in only if this is less than the maximum students in the class and what I'll do is I'll actually return true if the student was added successfully and then down here I'll return false if not so that we can tell maybe if we make a program down below if that student was added properly or not now I'm gonna add another method and here I'm gonna say define yet average great now wolf code that one out in a second but let's do an example of how we can actually now add students to these class so I'll make that a bit smaller so we can read it so let's make a few different students so let's say s 1 equals student what do we need to pass for student and named agent grades let's call that Tim let's call that 19 and let's call that 95 Tim's a smart guy all right and then we'll do s2 equals student we're gonna say bill we'll put him at 19 as well and he's not as smart he is 75 and then let's do s3 equals and let's go Jill keep with the trend Tim billed Jill 19 and maybe she has a 65 ok so we've created three students here and these students are proper they should work fine let's run this make sure everything's all right now how can we actually add them to our course well the first thing we need to do is make our course so we're gonna say course equals course like that what do we need when we make a course we need a name and we need a maximum amount of students so let's name this course let's say science or something like that and let's say the maximum amount of students is actually gonna be 2 because I want to show what happens when we add a student past the maximum now we're going to add students so how do we add them well we have to call that method so course dot add students so there we go we can add student s1 and then let's do course dot add student again and let's add s2 so now let's run this we see everything works fine and let's actually make a thing that can you know print out some thing about our student or can show our students so what I could do is I could say let's print down here course dots and this students and let's have a look here and we get main student object on some gibberish location main student object at some gibberish location so what this is actually telling us is both of these things inside our list right now our student objects and notice what I can actually do is let's say I index the zeroth item in that list so the first student that we added and I decide to call dot name on it well then that should hopefully print out the name of that first student which is 10 so that's cool and that's how we do that right we can add things in to this course so now this course stores all of our students and since all of these students have a grade on them inside of our 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 of the students that are enrolled in the course to do that what we're going to need to do is grab all the students from the students list we're gonna need to add that to an average and then we're gonna need to divide that value to figure out what that is so what we'll do is we'll say value equals 0 we're gonna say for student in in this case self dot students then we're gonna say value plus equals student the duck gets grade like that now notice that we could just type great if we wanted to but I usually just like to use the method because say we were to change any attribute later then this code wouldn't break so long as this method was still named the same thing and we can modify the method in here so they returned the proper grade like say we had a student enrolled in multiple courses then we could you know determine their grade in a different way and when we called get grade maybe we don't just return self grade we return something different so this still continues to work so we'll say value plus equals student get grade and now all will simply do is return in this case value divided by V Len of self dot students so let's see what the average grade of our courses so course get fabric grade will actually print that out like that and run that and we can see that the average grade is actually 85 and that makes sense with the grades that we have here so that's the idea behind what I'm trying to show you is that we have you know 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 which in fact what we could do is we could make another course we can add the same students to it and then we would obviously have to change the way that their grade was calculated but that's something that we could do now let's just look at what happens when I decide to try to add that third student so course start add student like that and we decide to add student s3 we can see that we get the value false and notice that the average grade does not change because we didn't actually add that into the course right okay so we finished off the basics on classes and objects and how to create our own classes and hopefully that last example helped you to really kind of understand the advantage of doing this now what we're going to talk about here is something called inheritance now this is where we slowly start getting to a little bit more complex and some more difficult concepts so try to follow along but I don't find inheritance as that extremely difficult so the idea behind inheritance and we'll show what that is in a second is you have two classes that are very similar so let's say we have a general class called maybe alright let's do a better example let's say we have a class called dog and a class called cat and in fact let's actually code these out so that we can see this for real so let's say in the init method of the cat what we're gonna do is we're gonna have self name and age like that so we're going to say you know self dot name equals if I can type properly which apparently is not happening right now and then self dot age and then we'll do define speak and all we'll do in here is just print you know what is this cat yeah okay and then let's say we have a dog so we'll say class dog like that and we'll define underscore underscore net on our squander 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 define here speak and the only difference between these two classes is actually gonna be the fact that this prints bark instead of meow so notice that these two classes are almost identical in fact there's only one line of code that's different other than the class definition at the top so there must be a way that we don't need to write this twice that we can actually use what's called inheritance so that these dog and these cat classes can well inherit from an upper level class which means that all that functionality is defined in one place and we only need to write what's different about those two classes inside of them so ideally what I would like to have is to be able to remove this init class from both of these and just have the speak method because the only thing that's 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 get show how we can actually do them so by removing that you know was an it method and just having these methods here what we need to do is make an upper level class which I'm actually gonna call pet so I'm gonna say class pet what I'm gonna do in here is define that an it method that we had before I'm actually gonna define a let's say show so this show method is just gonna show me all the things about my objects so I'm gonna say print and in here we're gonna say you know I'm actually gonna use an F string you may not know what that is but don't worry about it I am self taught name and I am self dot age years old okay so what I've done is I've defined this pet class and this pet class essentially is going to contain the functionality that I want the cat class and the dog class both to have and then in sign of the cat class and inside of the dog class what I'll do is I'll define the methods or the attributes or whatever it is that I need to do that are going to be different for this specific class so notice that pet is general we call this a generalization whereas cat and dog are more specific so how do I actually allow the cat class in the dog class to use this functionality well what I can do is simply add brackets and write pet now what this stands for is I am inheriting the upper level class pet so we're saying this is the general class this is a more specific class that's being created and inherits from pet and the same thing with dog let me show you how this works so let's create an instance first of all of the pet class and then we'll make one of the cat and the dog class and I'll show you how it works so let's say P equals pet notice that for pet we need a name and we need an age so let's say it's him let's put him at 19 and then let's do P dot Show so let's look at this year I am Tim and I am 19 years old so that is how the pet class works pretty straightforward now let's make a cat class so let's say C we're gonna say C equals cat I'm gonna say the cat's name will be Bill and that cat will be 34 like that and then let's do the same thing here see nutshell now notice that even though there's no method called show inside of cat it still pops up and says I am Bill and I'm 34 years old that is because it inherits inherits the properties from the pack class because I've defined that up here and notice that even though I didn't defined in an it method in here this still worked fine right there's you know this was okay we initialized because we use the anit method that was defined inside a pet and of course we can do the same thing with dog so we can say D equals dog what do we call this one before Jill 25 and let's do D dot shell like that we go I'm jail and I'm 25 years old so that works but now let's show what happens when we call speak on the cat and the dock so if I decide to call speak like that and we'll call it on both of them here you can see that we get me out and we get bark and again that's because this speak method is different for the cat class and different for the dog class and since its defined inside of here and we created an instance of cat when we make a cat instance well we're gonna use the speak method that's defined here and in fact what I could actually do is define speak up here and I could say define speak and I could say print I don't know what I say like that okay and then if I decide to change this from show to speak notice that we're calling speak three times speak is defined here and it's defined in both of our child classes is what we call them so when they when this is the upper level class the more general version any classes that inherit from it are known as the child classes or the derived classes um that's not that important to know but just you know figured out through that lingo out there and 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 the child class that is the same name as the upper level class it will automatically override that method so it will take over anything that's defined in here is more specific to this class so it will use that rather than using this upper one right and you might ask well why would we even bother defining one in here if it were just gonna take it over later well we might create another pet right maybe like a fish or something for example like I can do something like this class fish pet like that I can literally just define that and put pass inside of here and now what I can do is say 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 speak and notice that we get I don't know what to say for this fish class because there was no speak to find inside of here it used the speak that was defined in the upper level or parent class right the one that we inherited from so that is the basis behind inheritance now it gets a little bit more complicated I'm gonna show you the more complex out aspects because let's say I want to add one attribute to my cat so let's say that for cats we actually care what color those cats are as well well what I would need 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 this whole thing so what I'm gonna do and I'm kinda need to rewrite the whole thing but you'll see why we would actually do this in a second I'm gonna say self name age cover like that and now all I'm gonna do let me say self dot color equals color so what some of you may say here now that we've defined the color in here is that what we should do is take this name and age right just copy it and paste it in here now that would be a correct answer but I'm gonna tell you why we shouldn't do that so the idea here is that sometimes in the initialization method of our parent other things are happening other than just redefining attributes right and in that instance it would not be correct for us to simply omit the fact that we're not gonna call this a net from the parent we're just gonna define the attributes because that could mean that we miss out on a very important you know function that's happening from inside of this initialization to give you an example like saying some web applications maybe this initialization actually calls a database right and an ask for some information for a database and it sets up the object using that it wouldn't necessarily be correct then for me to just you know take the attributes that we're in here and just redefine them as attributes here I would actually still need to call that initialization to make sure the object was set up properly so to ensure that that happens when we do an inheritance like this we do need to define the arguments that we need are the parameters that we need for the parents initialization so name age but there's a fancy way to call that so rather than you know rewriting this here I can actually explicitly call this and set up our object that way so to do that I'm gonna say self are super underscoring square net done underscore underscore name age now what this is saying is super stands for reference the superclass and the superclass is actually the pet class or the class that we inherit from here so that's what super stands for the class that we are have inherited from then underscore underscore net underscore underscore defines the method that we want to call right and then name and age are the arguments that we're going to pass to that so name and age notice I don't need to pass cell that's fine we don't self and it's gonna call that and what's gonna happen is it's gonna run whatever's inside this initialization then that's gonna set up the name and the age for our object so we will have those properties defined and then we will call self duck ah they're equals color or we will execute that line so now we have the cover right and if I just go ahead and want to define here show so I want to change this show method maybe for this cat object then what I can do is I say I am self that name and I'm self that age and I am and in this case self dot color right so we can change the show method in here and now let's go to cat let's change this to show let's add a color because we need to add a color now we have to find that in the Annette and let's run this and see what the issue is now that we're getting fish is not defined oh did I do I guess I've deleted fish or we got rid of it at some point I did not remember okay so anyways let's run this and we can see that we have I don't know what to say I'm Bill and I'm 34 years old and I am brown and then we get bark right so that is how that works for inheritance and this is how we call the super class or the upper level parent class right we need to call this initialization method before we just go ahead and do anything else because that parents initialization may be important it may do other things it may call another method right so we can't just simply skip that we need to call that explicitly by writing this line now this line is kind of complicated syntax you know it's easy to forget but try to remember it super again references the parent class pet and then we have an it and like that so I hope that gives you an idea on how inheritance works now it's hard to give really good inheritance examples without going more complex and more into detail so I'm gonna admit doing that for now but just remember that when you have classes that do a very similar thing they have almost everything identical except maybe a few attributes or a few methods it might be a good idea to what we call generalize and make a parent class that is a general class that defines functionality will be used in all of your child classes and that is a very common practice and object-oriented programming to use inheritance for example a very good example is for example a good example is something like person hierarchy so let's say you have you're working in an organization and the example we want to consider is we have managers and we have employees now managers have different access than employees but employees and managers have very similar properties they all have a name they'll have an age they'll have an ID they have a birthdate they have many different things that they are the same for both of them well if we were trying to model that system and we were gonna program that and make that what we would likely do is create an upper level general class called person that defines all of the attributes and all of 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 that are different from each other so that's the idea behind inheritance hopefully that makes sense and now let's move on to our next topic okay so now it's time to talk about static and class methods and class attributes and in fact we're actually 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 right and inside the class we had self every where self was referring to the instance in which we were talking about in that you know context right so here what we're gonna do now is talk about 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'm gonna do a basic example where I just create a class person and I'm gonna say number of people equals 0 now in here I'm gonna define the NIT method and say to find an it like that and we're just gonna say each person will simply have a name keep it nice and simple like that so self that name equals name now let's make p1 person we'll talk about what I've done in a second here in case anyone's confused and let's say p2 equals person let's make this Jill okay so we have this number of people thing and I'm sure a lot of you're like what the heck is that well this is a class attribute and the reason it's not a regular attribute is because it does not use self so because it's not defined inside any method because it does not have access to an instance of the class it is defined for the entire class which means that this is not specific again to any instance it's not gonna change with from person to person whereas we know something except that name will be different for each instance of the person class this is not different for each instance of the person class in fact it's the same so what I can show you is that I can actually go ahead and print say you know p1 dot 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 right person dot number of people and the reason I can write personally because again this is specific to the class not to the instance so we can access it by just using the name of the class and that actually means that I can change it using the name of the class as well so person dot number of people equals 8 right and then if I go ahead and say okay P 2 dot number of people notice that we get 8 even though I didn't explicitly change it on P 2 it changed for P 2 because this was specific to the class and when I reference this all this says is when I say P 2 number of people the way that Python interprets this is what is the type of P 2 okay that's person notice you can see it's popping up here it says person then let's say does it have an attribute called number of people know this person itself does not does the class have an attribute called number of people yes it does let's display back and since we change that to 8 that's why we're getting that value and of course if we do this for P 1 like we've shown right we'll get the same number we'll get P 1 number people get 8 and if we decide to change this halfway through right so we do person equals 9 obviously that will become 9 now just because we changed it right before we printed the next value so that is the basics we be behind class attributes there's a lot of different uses for them now in this case what I want to do is have a number of people so what I was gonna do is inside of here say person number of people plus equals 1 so that we keep track of how many people or how many instances we have created of this class person so now if I decide to print P you know person dot number of people and we'll do that after we create the second person as well see we get one two and this automatically increments it so that's a basic example of one you would use a class attribute they're not extremely you but you know is something that you may want to consider and sometimes say you want to define a constant something like maybe gravity or something that's going to apply to every single person that you want to be a constant value then you define that as a class attribute so that if you ever decide to take this class and use it somewhere else that constant is still defined as opposed to putting it up here like as opposed to saying gravity up here and making it equal to you know negative 9.8 meters per second at the top what you would do is you would make that a constant inside as a class attribute so that now every time you want to access the gravity property for you know a person what you would do is you can reference directly the person's class constant of gravity rather than a global constant which may not be there if you put this class in a different file and that's the idea behind this these classes as well is that they are exportable I can write a class in one file and I can take it and move it to another file and hopefully it should continue to work assuming it does not depend on anything from the previous file so ideally you want to make your classes as robust as possible which means that they don't need anything outside of their initial class definition unless that's gonna be another class that maybe it's interacting with like in the example before we had course and we had student but that is the idea behind class attributes now let's talk about class methods so class methods are defined a little bit differently than regular methods and in fact I'll show you how they work we have a good example kind of set up here so I'm gonna say define and in this case we're gonna say number of people like that instead of saying actually self we're gonna say CLS then what we're gonna do right here is return CLS number of people and we're gonna use what's called a decorator to denote that this specific method is a class method and to do that we write act class method directly above the function or but yeah I guess function method whatever you want to call so I know this seems a little bit weird but the idea behind this is this method here is not going to be acting on behalf of one instance it's not gonna be specific to an instance and in fact you can call it on an instance if you want but that's not really going to be very effective what this is meant to do is be called on the class itself so that it can deal with something like you know returning the number of people that are associated with this class so these are class methods they that means they act on the class itself they do not have access to any instance and that's why I've written CLS here instead of self because there's no object what it's doing is just acting on this class so for example say we wanted to add to the number of people then what I could say is class method define add person like that CLS and we'd say CLS dot was it number of people like that plus equals one so that would be these are class methods we denote them with at class method just so we know that they're not referencing you know itself like that they're referencing the class so let's actually have a look at how we use that now sure need to necessarily print the number of people anymore what we can do is we can go down here and say person dot number of people and I don't know why there's so many brackets showing up there geez but if we look at this uh what we would need to print it out first we should hopefully get the value of 2 so let's look okay so we need to rename this so that this is not the same as the attribute because that's gonna be confusing so let's add an underscore there because I was realizing what the heck what's going on there and we can see that we get 0 oh so that is because I did not continue to add here so actually what I'm gonna do is and this actually be a good example so let's illustrate this here is I wanted to say too but I forgot that I forgot you know didn't continue adding this but what I can do is actually use the class method that I've defined here to add a person so I can say person dot add person like that inside of my in it and what that will do is call the class method on the class person and then it will add to the number of people so now when we run that we get two so that is 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 does not access any specific instance it only accesses these class attributes or anything specific to the class itself ok so that has big class methods now let's get rid of those and let's talk about static methods so sometimes I'm actually in delete the entire thing you want to create classes that kind of organize functions together so for example you know when you say like import math like that and then you get access to all these math functions like math dot abs or math dot square roots or whatever it is you're gonna use well what they sometimes end up doing in an object-oriented programming this is pretty common is when you have a bunch of functions that you would normally just define like you define like add one like this yeah I would do something you define add two like this well what you want to do is you want to actually organize them into a class and the reason you do that is just so it's a it stays a little bit structured you can move all those classes together to another module and continue to use 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 I'm gonna do in here is I'm gonna define some methods or some functions that I'd like to be able to use but that are not specific to an instance so I don't want them have to make an instance of this math class to be able to use these methods I want to be able to call them at any points and it doesn't matter if I have an instance of the math math class or not I would like to be able to use them so what I'm gonna do is actually create what's called a static method now static means not changing right it means staying the same and that is a really good way to describe what these methods do because they do not have access to an instance just 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 in here what we're gonna do is just say define add five and in here I don't even need to put a cell for a CLS because this is not going to access anything all its gonna do is just act as a function that is defined inside of this class and again some of you I'm sure are yelling saying what's the point of doing that why don't I just define a function globally well it's more of an organizational thing and there's some more specific applications in which you would use a static method so I do need to show them to you so here we're gonna take a number and all we're gonna do in this method is return X plus five so now if I want to actually use this what I can do is I don't need to say like M equals math like that and make an instance that's not necessary I can simply write the name of the class and math dot add five let's put five in here let's have a look if I got rid of the s and we get the value ten so this is called a static method and we can make as many of these as we would like just as we can make as many class methods as we want so maybe we do add ten right and then we have bat like that and we can change this method to say add 10 and now if we run this we get 50 so that is a static method notice it doesn't need anything in fact what I can actually do is say define you know PR let's say that's gonna stand for print let's make this an @ static method and now let's just print run like I'm just showing that you that you don't need any attributes in there are arguments and let's just call PR and now you can see that we get run and since I printed the value of that it prints them none but there we go like that okay so that is static methods and class methods and to be honest with that that is pretty much everything you need to know about classes and objects at least at a beginner level now there is some more interesting things that we could talk about but in the idea of keeping this more for beginners and so that everyone can kind of understand doesn't get to confuse I'm number frame from discussing anything further but I hope this really gave you a fundamental knowledge of how classes and objects work in Python a little recap here is to remember that everything we work with is an object in some sense we have functions which are objects we have strings which are objects integers are objects and what an object does is it a in instance of some class and that class defines the properties and almost kind of the blueprint 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 together and a class the type of an object is very important because it defines the behavior in which it can exhibit so that has been classes and objects in Python and introduction to object-oriented programming I hope you guys enjoyed if you did please make sure you leave a like these videos are not that easy to make and they are definitely time consuming so I would appreciate it subscribe to the channel and of course let me know if you have any questions or if there's anything you would like to see in the future
Info
Channel: Tech With Tim
Views: 2,269,294
Rating: undefined out of 5
Keywords: tech with tim, 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: JeznW_7DlB0
Channel Id: undefined
Length: 53min 5sec (3185 seconds)
Published: Sun Mar 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.