Learn Python OOP in under 20 Minutes

112.09k views3309 WordsCopy TextShare
Indently
In this video we're going to be learning the fundamentals of OOP (Object Oriented Programming) in Py...
Video Transcript:
this video was brought to you by IND dentle IO learning python Made Simple how's it going everyone in today's video we're going to be learning about classes in Python what they are and how we can use them but before we do anything I'm just going to explain a bit of the theory and to keep it simple a class is just a blueprint for what an object should look like and how it should function for example before you build a house usually you would create a blueprint containing all the relevant information and functionality regarding how that house should look and how it should function before you start building it and classes are no different they contain all the relevant information for what an object should do so for this lesson we're going to pretend that we are a microwave manufacturer which means first we're going to design the blueprint and then we're actually going to create the microwaves using that blueprint now in Python if you want to create a class you use the class keyword followed by the class name and the class name conventionally should follow camel case which means it will start with uppercase and every additional word will also start with uppercase so if you had a class called blue car you'll see that each time you have a new word it starts with an uppercase character but in this example we're going to call it microwave and for now it's going to be empty but already with this code we can start creating objects from this class because now we have the blueprint which means that we can actually get started with creating some microwaves so here what we're going to do is instantiate a microwave and to do that we're going to create a variable called Smeg which is a brand or a type of microwave you can search it on Google it's a very nice brand and that's going to be of type microwave which will equal the microwave with the Constructor which are these parentheses once you use the parenthesis on a class this creates an instance of that class which means we're creating a unique microwave from this blueprint also it's worth mentioning you did not have to include this bit here this is what they refer to as a type annotation or a type hint it's only used for code editors so if you don't like type annotations you can safely ignore that in all my videos I use type annotations because I find them very useful and I will use them throughout this lesson as well but it's up to you whether you want to include type annotations or not in your code anyway now we have an instance of a microwave which is called smag which means if we were to print this what we're going to get back is a unique ID pointing to the unique instance of this microwave but as it stands this class is kind of useless it doesn't contain any useful information nor functionality so next we're going to modify it because we want our microwaves to be be a bit more customizable so next we're going to be learning about initializers how to create instance with specific information which actually kind of customizes each one of the microwaves that we create so here we're going to be using a Dunder method called init and by default init returns none you're not required to provide this I just like to do it with all my code because it makes it extra explicit the initializer takes care of initializing the class which means if you have any extra information you want to give the class when you are creating an instance that code is going to be run here as soon as you instantiate it so right after Self we're going to type in brand of type string and power rating of typ string and inside here we need to type in self. brand equals brand and self. power rating equals power rating and immediately we're going to get some syntax highlighting because we need to now provide this information to the Constructor so here we're going to give it the brand of Smeg and we're going to give it a power rating of B and thanks to this information we now have a microwave that actually contains some data so if we were to print sm.
brand and sm. power rating you'll see that this class will contain that information it's going to return to us the information that we inserted and we can even create another mic microwave which we're going to call Bosch or bch and that's going to equal this microwave with the brand set to Bosch do let me know in the comment section down below how much I'm messing that up and the power rating will be set to C then we can print bch do brand and the power rating and what we should get is the information from Bosch now but right now you're probably asking okay that makes sense but what is self you completely skipped self well self is the actual instance of the class and that just makes sure that all this data sticks to the correct instance or to the correct object in the case of smag as soon as we insert this information self turns into smag so all of these become smag but that's only when we're referring to smag otherwise if we're referring to bch this becomes bch which means when we call b. brand it gives us the correct data back so again self just refers to whichever instance we're currently using and one thing that's quite cool that a lot of beginners don't know about is that you can actually change this name to whatever you want you can even change it to this which is something popular in Java or you can change it to something called instance if that's what you want you can do it I do not recommend you do that because conventionally we will always have this as self if you look at other tutorials or you look at documentation online this will always be referred to as self but it's just a convention so if you really want to change this to something else you can do that and the code will function the same I mean you can even change this to S and if we run the code it's going to work exactly the same way next let's move on to the topic of methods because right now we have a microwave that contains data but it doesn't do anything it doesn't function so what I'm going to do is remove all of this we're still going to keep Smeg because I want to use it later but the rest we're going to delete and inside here we're going to create our very first method which is going to be called turn on because we want to be able to turn on the microwave and that's going to return none because this will only execute code and I also forgot but inside the initializer we want to add another attribute and this one's going to be called turned on so we can follow or track the state of the microwave whether it's turned on or off and this will be of type Boolean and it's going to be set to false initially so as soon as we create a microwave we're going to make sure it's turned off as soon as we use it I mean imagine you open a box with a microwave and it's already turned on that would be kind of weird anyway now that we have that attribute we can refer to it by typing in if self.
turned on so if it is turned on we're going to print the following statement microwave and inside here I want to add the brand self. brand is already turned on so we don't turn on that microwave twice else we're going to set self. turned on to true and we're going to inform the user that we successfully turned it on so we'll copy this paste it down here and say that the microwave with the selft brand is now turned on and that will be our very first method but next I want to copy this and paste it directly under because we're going to create a function that turns off the microwave so we're going to rename that to turn off and this time if the microwave is turned on we're going to do the exact opposite we're going to set self do turned on to false and we're going to kind of flip these so I'm going to or actually to make this much easier I'm going to remove everything so we don't mix it up and just copy the statements from above so now we can type in that this is now turned off else if we try to turn off a microwave that's already turned off we're going to give them a statement that says the microwave is already turned off and finally I'm going to provide one more method for this class and this is going to be the run method so we can actually put food inside and give it a timer and cook that food so here we'll say run for this amount of seconds which will be of type integer and once again that will return none and obviously we first need to check if self.
turned on is set to True otherwise we can't run the microwave but if it is we can print that we are running self. brand for seconds seconds else we're going to print that a Mystical Force Whispers turn on your microwave first and those will be the methods that we will use for this class now we can actually turn on the microwave we can turn off that microwave and we can run the microwave as you can see we have a full blueprint of what the class should look like and how it should function so next let's try to use this functionality so right below our instance of Smeg we're going to refer to it and use dot notation to actually use these methods so right now we're going to turn on the microwave twice and what you're going to notice in the console is that the first time it's actually going to turn it on but the the second time it's going to tell us that it's already turned on so we have that safety mechanism in place but once it's turned on we can refer to Smeg and run this microwave for let's say 30 seconds or yeah 30 seconds and then we will turn off the microwave and now if we run it you'll see that the microwave will turn on it's going to run for 30 seconds and then it's going to turn off because that's exactly what we told it to do if we try to run our microwave after we've turned it off for 10 seconds we're going to get that Mystical Force that Whispers to us that we should turn on our microwave first so as you could see it was that easy to give our class functionality and data that we could later use to execute certain functionality and what's really cool about this is that no matter what microwave we create we can use all of that information the same way which means here we're going to change it back to bch give it a power rating of C and now with B we can perform the same actions we can turn it on we can turn it off and we can try to run it for let's say 2 seconds and it's going to work exactly the same way microwave buch is now turned on and then it turns off and then the Mystical Force Whispers to us that we can't run it without turning it on first so with that minimal setup we could use all the functionality once again but there's still one last thing I want to show you before we conclude this video and this is the concept of Dunder methods so for the next example I'm going to remove all of this and keep both of these microwaves so that we can use them later also in pycharm I'm going to collapse these methods because you already know what they contain so that we have some more space on our screen but now that we have all of this space on our screen I can finally get started with explaining how Dunder methods work in Python and to explain it I'm going to attempt to perform this operation which is Smeg plus bch if you try to run this you'll immediately notice that we will get back a type error that we tried to use this operand with these objects and that was not supported because we did not Define this operation anywhere in our class but to Define it what we need to do is go inside our class and Define a Dunder method so here we're going to use the dunder method called add which will take self which is the current instance and which is the other instance and the reason they call this thunder method is because they have double underscores so it's actually a double uncore method and you might also hear these being referred to as magic methods anyway as soon as you supply this thund method the syntax highlighting will go away and the warning will go away as well right now if I don't add anything here the script will run but it's not going to return anything so adding these together will do nothing but inside here you can actually do whatever you want you can make it return whatever you want if you add one microwave to another you might expect to get a fusion back and in this example we can return a string with self. Brand Plus other do brand so that when we actually try to add these together what we will get back is this Fusion and the same thing applies for the other operators whether it's an asterisk or a pipeline or whatever all of these can be defined in our class so if we want to Define for example the multiplication operator we can do that using the dunder mole method and here we can return exactly the same thing from earlier except with an asterisk as you can see now the next time we actually call this it's going to give us back the string that we specified and you don't have to return a string here you can actually return whatever you want it can even be another instance of the class but one that's actually very useful is the string Thunder method and this is going to return to us a string and what we will return is the F string of self.
brand parenthesis with the rating set to self. power rating and what this does is give us a legible string representation of our class so that the next time we actually print any one of our classes we will get back something that we can actually read and if we do this with bch we will also get something legible for B otherwise without this Stander method we're going to get the memory address of the class or the representation of the class and that's also something that you can override for example if we go here and we Define the representation this will return to us a string so here instead of returning that memory address you can return whatever you want this will be used when you use the representation of the class so if you use repper on Smeg or repper on B it's going to use this thunder method but you might be asking if we had none of these defined why do we get this back for the representation well that's because that's the default representation for a class when you print an object if it can't find the string Dunder method it's going to use the representation that's why when we use the string representation it's going to fa to using the string representation and one thing I want to mention very fast because I know this will confuse a lot of people when they are learning python but the difference between the string Dunder method and the reer dunder method is that the string Dunder method should return something that is user friendly something that your grandmother could read while the representation Dunder method should return something that's useful for the developer for example here we have a microwave class so something you could return is the microwave with the following arguments which we have brand and power rating so brand can equal self. brand and power rating will equal the self.
Copyright Ā© 2024. Made with ā™„ in London by YTScribe.com