Object Oriented Programming with Python - Full Course for Beginners

freeCodeCamp.org · Beginner ·🛠️ AI Tools & Apps ·4y ago

Key Takeaways

This video tutorial covers Object Oriented Programming with Python, including classes, constructors, class and static methods, inheritance, getters and setters, and OOP principles.

Full Transcript

it's important for software developers to understand object-oriented programming in this course jim from gym-shaped coding will teach you all about object-oriented programming in python object-oriented programming it could be what is holding you back from being a great python developer and as well as landing your first job as a software engineer welcome everyone to python object-oriented programming course now if you struggle to understand the concepts of object-oriented programming in the past then you are totally fine and you are in good hands because in this course i'm going to make sure that this will be the last tutorial that you will ever watch about classes and the complex concepts that comes with object oriented programming and we are going to do this by developing a real python application that is going to be very cool to write and we will add to its complexity step by step and throughout the way we will understand everything that we need to know about object-oriented programming now there are going to be some requirements to this course i do expect from everybody to know at least about functions variables if statements and as well as for loops and if you know those things from other programming languages then this is also fine so with that being said let's get started now to explain why you should write object oriented programs i will explain the concepts based on a store management system that we will start developing together so starting to think about how to take our first steps with such a program we could first think about tracking after the items that we have right now in our store so one way we could get started we could create those four variables to start tracking after our items so as you can see we have our first variable item one equals to phone and then we have three more variables that are intentionally starting with the prefix of item one so that we could describe that those four variables are related to each other by following the correct naming conventions now you might think that those four variables are related to each other only because it uses the same prefix of item one for python those are just four variables with different data types so if we were to print the type for each of those four variables now we would receive their types with no surprises right we will receive string and integer for price quantity and price total now i want to focus on those specific outputs right now because as you can see for each of the types we also see the keyword of class now this means that those data types are actually instances of strings or integers so in python programming language each data type is an object that has been instantiated earlier by some class and for the item1 variable that has been instantiated from a string type of class and for the price quantity and price total those have been instantiated from a class that is named int meaning integer so it could have been nicer if we could tell python that we want to create a data type of our own it will allow us to write a code that we can reuse in the future easily if needed now each instance could have attributes to describe related information about it and we can think about at least some good candidates for attributes we could have for our item data type like its name price or quantity all right so let's go ahead and start creating our first class so i will clean everything from here and we'll go ahead with it so it is going to be divided into two parts the first one will be the creation of the class and the second one will be the part that i will instantiate some objects of this class now when i say creating an instance or creating an object basically i mean to the same thing so you might hear me saying one of those all right so let's go ahead and say class and then this needs to be followed by the name of the class that you want to create so we would like to give it the name of item and then inside of this class in the future we are going to write some code that will be very beneficial and very useful for us so we won't repeat ourselves every time that we like to take similar actions but for now temporarily i'm going to say here pass so we will not receive any errors inside this class definition all right so now that we have created our class then we are allowed to create some instances of this class so let's go ahead and say item one is equal to item and that action is equivalent to creating an instance of a class just like if you were to create a random string then you will say something like the following this is equivalent to this one as well so it is very important to understand how classes are working in python so i will delete this line because this was just for an example and now i said that we are allowed to assign some attributes to instances of a class so let's go ahead and start creating attributes and that will be achievable by using the dot sign right after the instance of a class and here you can say that you want to give it an attribute like a name that will be equal to phone and item1.price could be equal to 100 and item one dot quantity could be equal to five for example now in that stage you might ask yourself what is the difference between the random variables that we have created to those four lines well here we actually have a relationship between those four lines because each one of the attributes are assigned to one instance of the class and i could preview this by going ahead and try to print the types of item one now and as well as the types of the attributes of name price and quantity now with name price and quantity we are not going to have any surprises because we assigned string type attributes to the item object but if we were to print that then check out the result if i was to run this program so you can see that now we have a data type of item here and that is the big difference between what we have seen previously to this thing that we have just created so now we understand how we can create our own data types now let's go ahead and see what are the rest of the benefits using object oriented programming okay so until now we understood how to assign attributes to instances we should also understand now how we can create some methods and execute them on our instances now if we will take as an example the built-in class of string then you know that we have some methods that we can go ahead and execute for each of our strings and for this example you can see that i grab an instance of a string that i named random str and then i go ahead in the next line and execute the upper method which if you remember is responsible to grab all the letters and turn them to uppercase now the biggest question here is how we can go ahead and design some methods that are going to be allowed to execute on our instances well the answer is inside our class so we could go inside our class and write some methods that will be accessible from our instances so we could go ahead and say def and give our method a name now a good candidate for a method that we'd like to create now is actually calculate total price because as we understand it could have been nice if we were to have a method that will go ahead and calculate the result multiplying item1.price with item1.quantity so we can get the total price for that specific item now before we go ahead and complete this function then i'm going to just create one more instance of this item by also deleting those two lines because we understood the example so i'm just going to change those to item 2 like that and i'm going to use something like laptop and change the price to 1000 and say that we have three of those now just a quick side note when you will hear me say methods then i basically mean to functions that are inside classes because in terms of python or in any programming language when you have isolated definitions with this keyword then those are considered to be called functions but when you go ahead and create those functions inside classes then those are called methods so that is an important point that you should understand because i'm going to call those methods from now okay so now if i was to continue by opening up and closing those parentheses then you are going to see one parameter that is generated that python wants us to receive intentionally now the reason that this happens python passes the object itself as a first argument when you go ahead and call those methods now if i was to go here and say item1 dot calculate total price then the action that we are doing now is calling this method but when you go ahead and call a method from an instance then python passes the object itself as a first argument every time so that is why we are not allowed to create methods that will never receive parameters now you will see this if i was to remove the first parameter and say something like pass now if i was to execute this program now then you are going to see type error calculate total price takes 0 positional arguments but one was given so in simple words what this exception says is that python tries to pass one argument and you are not receiving any parameter so that is very problematic and that is why you always have to receive at least one parameter when you go ahead and create your methods now since we always receive this parameter then it is just a common approach to call this self it was okay if i was to call it something like my param or i don't know something else but you never want to mess up with common conventions across different python developers so that is why just make sure that you leave it as self every time now if i was to go ahead and run this program then you're gonna see that we are not going to receive any errors so this means that this method has been implemented correctly now let's see how we are going to benefit from creating this method because it should go ahead and create a calculation for us using price and quantity so i will intentionally receive here two more parameters which we could name just x and y for now and we could just say return x multiplied by y and now i will go ahead and pass in here two additional arguments and it will be item1.price the second one will be quantity so that is going to work because when you call this method in the background python passes this as an argument and then it passes this second argument and then this has been passed as a third argument so that is perfect and if i was to run that and actually print this so excuse me for running this before printing it so i will surround this expression with this print built-in function and i will run that and you're gonna see 500 as expected now i could do the exact same thing for calculating the total price of our second item so if i was to grab this and paste this in in this line and actually change this to item two and change this one to item two and as well as this one then i will receive three thousand as expected and that is how you can create a metal alright so until that point we understood that we can assign attributes and as well as creating some methods that we can go ahead and use them from our instances directly like those two examples in that line and as well as in that line now in that episode we are going to solve some more problems that we have in terms of best practices in object-oriented programming and things that you are going to see in each project that is based on oop all right so let's get started now one of the first problems that we have here is the fact that we don't have a set of rules for the attributes that you would like to pass in in order to instantiate an instance successfully and what that means it means that for each item that i want to go ahead and create i need to hard code in the attribute name like those in here and it could have been nicer if we could somehow declare in the class that in order to instantiate an instance successfully name price and quantity must be passed otherwise the instance could not have been created successfully so what that means it means that it could have been a great option if we could somehow execute something in the background this second that we instantiate an instance and there is a way that you can reach such a behavior and that is by creating a special method with a very unique name which is called double underscore init double underscore now you might hear this term as well as called as constructor basically that is a method with a unique name that you need to call it the way it is intentionally in order to use its special features now the way that this is going to work is by creating it the following way so it will be double underscore and as you can see i already have auto completion for some very special methods that are starting and ending with double underscore now the collection of those methods are used to be called magic methods and we are going to learn a lot of more magic methods that you have in oop but the first one that we are going to start with will be the init double underscore like that all right so now that we have created this method then let's actually see what this method does in the background so when you go ahead and create an instance of a class then python executes this double underscore init function automatically so what that means it means that now that we have declared our class python is going to run through this line and then since an instance has been created and we have double score init method designed then it is going to call the actions that are inside this double underscore init double underscore method now in order to prove you that then i'm going to start with a very basic print here that will say i am created like that now we got here one instance and here we got another one so we should see i am created twice and in order to avoid confusions then i'm going to delete those print lines from here so we can see a cleaner picture all right so if we were to run our program then you can see that we have im created twice and that is because python called this double underscore init double underscore method twice thanks to those two instances that we have created all right so now that we use the double underscore init function in this class we should take benefit from it and solve some more problems in order to implement oop best practices now if you remember in the beginning of this tutorial i said that one of the problems that we have till this point is the fact that we still hard code in the attributes in that way by saying dot name dot price dot quantity and that is something that we can for sure avoid now let's see how we can start avoiding creating those attributes hard-coded for each of the instances here so we can actually benefit from the double underscore init method that we have designed and let's see how now we understand that for each instance that we will create it will go ahead and call this double underscore init method automatically so what that means it means that not only we can allow ourselves to receive the sales parameter because this is a mandatory thing that we should do because python in the background passes the instance itself as a first argument we could in addition take some more parameters and then do something with them so as a starter let's say that we would like to receive one more parameter that we could name it name and as you can see automatically python is going to complain how the name argument is not filled in here so now i could go ahead and pass in the argument of phone for that one and for the second one i can go ahead and pass in the argument of laptop now once i have created this then i can actually go ahead and change my print line a little bit so it will be a unique print line where i can identify from where each print line came from so i can go ahead and say an instance created and use a column here and then refer to the name like that and now that we have created this then if we were to run our program then you're gonna see unique sentences and instances created for the phone and as well as for the laptop all right so now that we have done this then there is something that is still not quite perfect because we still pass in the attribute of name here and here so now pay attention how the init method has to receive the self as a perimeter as well and we already know the reason for that and the fact that we have self as a parameter here could actually allow us to assign the attributes from the init method so that we will not have to go ahead and assign the attribute of name for each of the instances we create so what that means it means that i can dynamically assign an attribute to an instance from this magic method which is called double underscore init so if i was to say self dot name so i'm assigning the attribute of name to each instance that is going to be created or created yet and i'm making that to be equal to the name that is passed in from here so what that means it means that now i can allow myself to delete this line and then this line so as you can see now i have a dynamic attribute assignment thanks to this self.name equals name that we have wrote here and to test that the attribute assignment worked then i can go down here and use two more lines that will look like the following so i will print item item1.name and i will also print item2 dot name and in order to avoid confusions then i'm going to get rid of this line so we could only see the print lines from here and now if i was to run that then you can see that we receive phone and laptop so it means that we were able to assign the attributes dynamically and that is perfect and now that we get the idea of that then we should also do the same for the rest of the attributes that we'd like to receive so we also got the price and quantity to take care of so i'm going to go to my init method and i'm going to receive again price and quantity and i'm going to do the exact same thing so i'm going to assign the attribute of price and that will be equal to price and the quantity will be equal to the quantity and you can also see that again python complains about the price and the quantity not being passed in here so i can say 100 and then 5 and then i can delete those and then i can do the same here i could pass in 1000 and then 3 and delete those and in order to prove you that this is going to work then i'm going to copy myself a couple of times and change this to quantity i mean price this one will be price as well this one will be quantity and this one as well and now if i was to run that then you can see that the results are as expected so that is a way that you should work with the double underscore init method you should always take care of the attributes that you want to assign to an object inside the double underscore init method meaning inside the constructor now a couple of side notes that are quite important to remember when we work with classes now when we go ahead and use the double score init method this doesn't mean that we cannot differentiate between mandatory parameters to non-mandatory parameters so say that you currently don't know how much you have from a specific item then you can go ahead and by default receive this quantity parameter as zero because it is realistic situation that you currently don't know how much phones you have on your store so you can directly go ahead and use a default value for that for example zero and then this will mean that you will not have to pass in those five and three here and now in order to show you the results of that if i was to run our program then you can see that we received zero twice for those two prints in here so that is something that you want to remember and one more quite important point that i'd like to talk about now is the fact that you can assign attributes to specific instances individually so say that you want to know if the laptop has numpad or not because some laptops are not having the numpad on the right side of the keyboard but this is not a realistic attribute that you would want to assign to a phone and that is why you can go ahead and let me delete those print lines by the way and that is why you can go ahead and say something like item two dot has numpad equals to false like that and that is something that you want to remember because the fact that you use some attribute assignments in the constructor doesn't mean that you cannot add some more attributes that you would like after you instantiate the instances that you would like to alright so now that we understood this then there is still one small problem that is left that we need to solve now pay attention how the calculate total price still receives the x and y as parameters and the question that we ask now is why it still receives those parameters well we could for sure now not receive those parameters because as we know for each method that we design in classes then the object itself is passed in argument and i know that i repeated this couple of times but this is where i fail to understand classes so that is why it is very important to understand this behavior and we already know that the object itself passed as an argument so that's why we receive self and so this means that now we could just return self.price multiplied by self.quantity and this will mean that we don't really have to receive those parameters because we assign those attributes once the instances has been created so this means that we have access to those attributes throughout the methods that we are going to add here in this class in the future so in order to test that this works then i'm going to delete this example for now and i'm going to say print item 1 dot calculate total price so we will be able to return the result here and i will do the same for item two um sorry only this one now to show some real number other than zero then i will go ahead and pass in here quantities so i will say one and three for example because i don't want to multiply a large number with a zero and that could come from here so i will run that and you see that we receive the expected results so now we completely understand the big picture how to work with the constructors in classes and what are the best practices that you should go ahead and implement all right so now that we understood this then we might think that we have done everything perfectly but actually i want to show you what will happen if we were to pass in here a string besides an integer and run our program so if we were to run that then you can see that we are screwing things up here because this function for example thinks that it should print the string three times because you'll see we have 1000 multiplied by three that is being returned in here so it shows us one thousand once one thousand twice and then one more time so what that means it means that we have to validate the data types of the values that we are passing in so there are a couple of ways to achieve this and one way is by using typings in the parameters that you're declaring inside here so a great starter will be for example to declare that a name must be a string now let me first take this back and change those to integer and then go here and design those parameters so in order to specify a typing then you should go ahead and create a column sign followed by the type of the data type that you expect to receive here so if i was to pass in here only the object reference to the class of str then it will mean that it will have to accept strings only and i can preview that by changing this to an integer and you're gonna see that we have a complaint here that says expected type str got int instead and that is perfect so now that we have done this then i'm going to do the same for the price itself and price we could actually do the same thing with it by passing in float now when we pass float it is okay to also pass integers and that is something very unique with floats and integers together so that is okay to use the typing of float and for the quantity we don't need to specify a typing because the fact that we passed a default value of integer already marked this parameter as to be integer always so that is why for example if i was to leave this as it is and change the quantity to a string then you're gonna see that it is going to complain because the default value is already an integer so it expects for an integer all right so those things are actually great setups to make our init function more powerful but we might still want to validate the received values in the following way so say that you never want to receive a negative number of quantity and you never want to receive a negative number of price so that is something that you cannot achieve by the typings in here but there's actually a great way to work this around and that will be by using assert statements now a third is a statement keyword that is used to check if there is a match between what is happening to your expectations so let's see how we can get work with assert so i'm actually going to delete this from here and i'm going to organize our init method a little bit i'm going to say here a comment and i will say assign to self object and i will say up top something like run validations to the received arguments all right so now it is a great idea to validate that the price and quantity are both greater than or equal to zero because we probably don't want to handle with those when they are negative numbers and we want to crash the program so we could say assert and pay attention that i use it as a statement not a built-in function or something like that and i can say here price is greater than or equal to zero now once i set this then i can also do the same for quantity actually so let me do that quickly by this way and then once we have this then i can actually go ahead and run our program and you will see that i will not receive any arrows but the second that i change this quantity to negative 1 for example and this one being negative 3 then i will have some errors that will say assertion error now you can see that the fact that we see here assertion error is quite a general exception that doesn't mean anything now what is so beautiful with assert you can add your own exception messages right near of it as a second argument so let's go up top here and go back to those two lines so the first argument that is passed to the statement is the statement that would like to check but if we were to say here comma and use a string to say actually a formatted string and i can say price and then refer to the value of it is not greater than 0 like that i can add an explanation mark here and i can use the same thing copy that with a comma and paste this in here and change this quantity and then refer to the value of it and say that it is not equal to i mean greater than or equal to zero so it needs to be actually changed to greater than or equal to like that and same goes for here and i have some space here that should be deleted all right so now if i was to execute our program then you can see that we receive assertion error quantity minus one is not greater or equal than zero so i should delete this then here for that and now it is perfect so now we understand that using the assert statement could allow us to validate the arguments that we receive and also it allows us to catch up the bugs as soon as possible before going forward with the rest of the actions that we like to take within this program so let me actually change those back to valid values like that and that is perfect alright so until this point we learned about how to work with the constructor and we also learned about how to assign different attributes to instances that are going to be unique per instance which means that you can go ahead and create as much as instances as you want and you have the control to pass whatever values you'd like to for the name price and quantity now consider a situation that you'll want to make use of an attribute that is going to be global or across all the instances now a good candidate for example of this could be a situation that you'll want to apply a sale on your shop so this means that you want to go ahead and having the control of applying some discount for each one of the items and that is a good candidate for creating an attribute that is going to be shared across all the instances now we call those kind of attributes class attributes and the kind of attribute that we have learned till this point is actually called in a full name instance attributes so about instance attributes we know everything and we learned how to work with it but we did not work it with the other kind of the attributes which we will do in this tutorial which is called again a class attribute so a class attribute is an attribute that is going to be belong to the class itself but however you can also access this attribute from the instance level as well let's go ahead and see a good candidate for a class attribute that you want to go ahead and create it so that's gonna be going to our class here and just in the first line inside our class i can go ahead and create a class attribute so let's go ahead and create an attribute like pay rate equals to 0.8 and the reason that i'm doing this is because i said that there is going to be 20 percent of discount so i probably want to store an attribute that will describe how much i still need to pay so i will say here the pay rate after 20 percent discount like that okay so now that we have created this then let's see what are the ways that we can access this attribute now if i was to go down and actually deleting one of those and say something inside this print line that will look like the following so i will try to access to the reference of the class itself so i'm not going to create an instance like that besides i'm just going to bring in the reference to the class level itself and i'm going to try to access this attribute by saying pay underscore rate now if i was to run that then you're going to see that as expected we see this class attribute because that is a way that you can access those class attributes now this might be confusing but i said a minute ago that you can also access those class attributes from the instance level well let's see if that is true so if i was to duplicate those lines twice by using the shortcut of control d then let's go ahead and change those to item one and this one to item two now see how i try to access the pay rate attribute from the instance although we don't have such an instance attribute now if i was to run that then you're gonna see that we still have the access to see that class attribute well that might be confusing and that might be hard to understand why that is happening well there is actually something that we need to understand when we work with instances in python so when we have an instance on our hand then at first this instance tries to bring the attribute from the instance level at first stage but if it doesn't find it there then it is going to try to bring that attribute from the class level so what that means it means that item one did some thinking here and say to itself okay so i don't have this attribute right in here because that is just not an attribute that assigned to me so i'm going to try to search that from the instance level and then i'm going to find it and print it back so that is exactly what is happening here item 1 and item 2 are instances that could not find the pay rate attribute on the instance level so both of them went ahead and tried to bring this attribute from the class level and since it really exists in the class level then we were able to access those now to even give you a better idea of what is going on here then i'm going to do one more additional thing now i will delete this first print line and i will go ahead and delete those attributes from here as well now there is a built-in magic attribute not a magic method that you can go ahead and see all the attributes that are belonging to that specific object and that is achievable by using this double underscore dict double underscore like that so this will go ahead and try to bring you all the attributes that are belonging to the object that you apply this attribute and want to see its content so i will go ahead and copy this one and paste this in for the instance level as well so this should give me all the attributes for class level and the second line should do this for the instance level all right and if i was to run that then let's explore the results for a second now you can see that at the first line we see this pay rate attribute but in the second line we never see it we see name price and quantity and you can also pay attention that this magic attribute is actually responsible to take all the attributes and convert this to a dictionary and that is from where the keyword coming from it is just a shortened version of a dictionary so that is a very useful magic attribute that you can go ahead and use if you just want to see temporarily for debugging reasons all the attributes that are belonging to some object all right so now that we understood this then let's take it to a real life example and come up with a method that will go ahead and apply a discount on our items price so that will be by creating a method that will be belong to each of our instances and that means that we can go ahead and come up with a method that we could name apply discount so let's go ahead and start working on this so i'm going to say def apply discount and pay attention that i'm using a new method inside a class here so right inside of this then at first we need to figure out how we are going to override an attribute that is belonging to an instance and we already know that we can do that with the self keyword so it will be self dot price and that will be equal to self.price meaning the older value of this attribute multiplied by the pay rate now you might expect that we could access this directly like that but if you remember that is actually belonging to the item class itself now this might be confusing because this method already inside this class so you might think already that you can access it directly by saying pay rate because it is already inside the class but that is actually not going to work because you can either access it from the class level or the instance level as we understood previously so i can go ahead and say item dot pay rate like that and there you have a method that can go ahead and basically override the price attribute for one of your items now to show you that this works then i can only use one instance for now and i can go ahead and call this method by say apply discount and i can also now try to print the attribute of price for this item one and we should see 80 right so if we were to run that then we're going to see that we are going to receive 80.0 as expected now we should not forget the option that you might also want to have a different discount amount for a specific item so say that one day you'll have 20 items and only for the laptop you want to have a 30 discount but it is going to be a bad idea changing the class attribute to 0.7 because it will affect all the items that you have right now on your hand so what you can do instead is you can assign this attribute directly to one of the instances that you would like to have a different discount amount for so let's go ahead and see an example for this so i will allow myself to bring back the item of laptop and then what i can do to apply a 30 discount for this item is assigning the exact same attribute to the instance so i can go ahead and use a item tool dot pay on the score rate is equal to 0.7 now what will happen here is that for item 2 it will find the attribute of pay rate in the instance level so item 2 does not really have to go ahead to the class level and bring back the value of pay rate because at first look it is going to find it in the instance level but for item 1 it is different it is still going to read from the item level which is going to be 0.8 so now if we were to try to use item two dot apply discount and as well as printing the price now then let's see what will happen so i will uncomment this line to not see this print for now and i will go ahead and execute our program now you can see that we still however receive 800 and what this means this means that the discount that has been applied is still 20 and where this is coming from well this is coming from this method here that no matter what we try to pull the pay rate from the class level so a best practice here will be to change this to self and that way if we overwrite the pay rate for the instance level then it is going to read from the instance level but for item one if we try to access the pay rate from the instance level then this is still great because we did not assign a specific pay rate for item one so it is going to pull that from the class level now if we were to try to run that then you're gonna see now that we have expected results and if we were to also uncomment the first print line for the item one and rerun our program then you can see that for item one we had twenty percent discount and for item two we had thirty percent discount so when it comes to accessing class attributes you might want to reconsider how you want to access them when you come up with some methods and specifically for creating a method like apply discount it is a great idea to access it from the instance level so you also allow the option of using a pay rate that is assigned to the instance level okay so now that we understood completely about the differences between a class two and instance attribute let's jump ahead to the next topic now you see that i have deleted those print lines that i have down below and i came up with five instances that i have created here so you might also want to create those five instances immediately so that is why i will recommend you to go ahead to my repository accessing this class attributes directory and then code snippets and then go ahead and copy the code from these five underscore items.py file okay so considering a situation that your shop is going to be larger in the future meaning that you are going to have more items then the more items that you are going to have the more filtration like things that you want to do in the future but what is problematic currently with our class is the fact that we don't have any resource where we can just access all the items that we have in our shop right now now it could have been nicer if we could somehow have a list with all the item instances that have been created until this point but currently there is not an approach that will give us a list with five elements where each element will represent an instance of a class so in order to come up with such a design then here is a wonderful candidate for creating a class attribute that we could name all and once we do this then we're gonna see how we are going to add our instances to that list so i will go ahead and start by going here and use an old attribute so it will be all equals to an empty list now we need to figure out how we are going to add our instances for each time that we are going to go ahead and create an instance now if you remember the double underscore init method is being called immediately once the instance has been created so it might be a wonderful idea going down below inside this double underscore init method and use a code that will be responsible to append to that list every time that we create an instance and that will be as easy as saying something like the following so first you could pay attention that i actually wrote some commands in this double underscore init function like run validations and assign to self object so it might be a great idea to start with a comment here that will say actions to execute just to really have a great separation between the different things that we are doing so now inside here i can say item dot all and you can see that i use the class object first and then that is a list so i can use dot append and then i will just append the self object now we know that self is actually the instance itself every time that it is being created so once we go ahead and launch such a command inside the init function then for each instance that is going to be created this whole list is going to be filled with our instances now to show you that i can jump a line after we create the instances and we can say print item dot all and now if i was to run our program then you are going to see that we are going to have a list with five instances if i was to scroll right a bit then you can see that i have exactly five elements and that is perfect now that's going to be extremely useful if you want to do something with only one of the attributes of your instances so say that you would like to print all the names for all of your instances then you can use easily a for loop to achieve such a task so we can go ahead and say for instance in item dot all and you can say print instance dot name and once we come up with this then you can see that we have all the names for all the instances that we have created so that is going to be useful here and there especially if you know how to use the filter function for example to apply some special things on some of the instances that are matching your criteria all right so now that we understood this then let's also take care of one problem that we saw previously now if i was to use a ctrl z a couple of times and still use this print item.all now you could see that the way that the object is being represented is not too friendly now it could have been nicer if we could somehow change the way that the object is being represented in this list here now there's actually a way to achieve this by using a magic method inside our class now there is a magic method that is called double underscore repr and repr stands for representing your objects so that is why you can actually go ahead and use this magic method and then you will have the control to display your objects when you are printing them in the console now i actually recommend watching a video that compares between a method that is similar to it which is called double underscore str and you can take a look in the description of this entire series to actually watch the video that i'm talking about all right so let's go ahead and use the repair method to understand how this is going to work so i'm going to say def inside our class and i'm going to use double underscore re pr double underscore and as expected it will receive the self now what we can do now is returning a string that will be responsible to represent this object now obviously we don't want to use something that is not unique for each of the instances because say that i was to use now return item something like that and run our program then you can see that i'm going to receive a list with this string five times but it is going to be hard to identify which instance represents each string here so it could be helpful if we were to return a string that could be unique so i'm going to close the console here and go ahead here and use a formatted string and in order to make this unique it is a best practice to represent it exactly like we create the instance like that so what i'm going to do here is leaving the item and use a brackets opener and the closure and then i'm going to make the return string here as much as equal as possible to the way that we create those instances so i will start by typing here single quotes to escape from the double quotes that are coming from here and i'm going to refer to the value of name by using self.name and then i will leave my single quotes and i will use a comma like that and then i will go ahead and refer to the value of our price i will use one more comma and i will say self.quantity now if we were to execute our program again then you can see that now we receive a list that is way more friendly than what we have seen previously and you can also see that this first element for example is quite equivalent to this line here now you might be curious why i worked so hard to return the representative version of our objects the same way that we create them so that is just a best practice according to python's documentations because it will help us to create instances immediately by only the effort of copying and pasting this to a python console so if you think about it right now if you open a python console and you import this class then it will be as easy as grabbing this and pasting to the python console and then you will have an instance being created so that is the single reason that i have came up with this approach and also for sure i just wanted to return a unique string that will really represent our instance and you can see that it is very easy to identify the instances of our class with this list and with this approach all right so until this point we understood how we can change the way that we represent our objects and we also understood how we can access to all of our instances by this class attribute that we intentionally named all now in this part we are going to take a look to solve one more problem that we have in terms of best practices when we are going to extend this application and add more features now you can see that until this point we maintain our data as code in this main.py file by only instantiating those five items now when we will look to extend this application and add some more features then we might have a harder life to add those features because the actual data and the code are maintained in the same location meaning in the same main.py file now you could have think about creating a database that will maintain this information but i want to keep things more simple for the purposes of this tutorial and that is why i'm going to use something that is called csv that you might have heard of csv stands for comma separated values so this means that you could go ahead and use a csv file and you could store your values as comma separated where each line will represent a single structured data and csv is a great option here because it allows the data to be saved in a table structured format all right so let's go ahead and create a csv file and i will actually go ahead and name this items.csv like that and i will go ahead and paste in some csv content that will be responsible at the end of the day represent the same data that we look to have here so you can see that at the first line i have name price and quantity and you can see that those are comma separated so those represents the columns that we are going to have as the data that we are going to maintain and in the second line and further we are going to have some data that will represent the actual data that we look to maintain so if we were to now split the panes then we can see that those are quite equivalent and now we should only look for a way to read this csv file and actually instantiate those objects now you can see that i have a suggestion by pycharm to install a plugin that will support csv files so i'm going to just click on that and install those plugins and you can see that i will have a csv reader here and we will see if we will be able to see this data in a table which will be a lot nicer so let's go ahead and install this and now you can see that i have some more options that i can actually go ahead and use from here i know that this is quite small but actually you have some tabs that you can go ahead and click on them and if i was to click on table editor and actually give this file more focus then you can see that i actually have the best way to read this data now you can see that i have my columns you can see that i have my rows and that is quite nice now i can really go ahead and visualize my data more nicer and it is just more common way to maintain your data okay so now that we understood how csv files are working let's go ahead and read our csv files and instantiate the instances in a generic way so it makes sense to delete those five lines and i'm going to use those lines below the apply discount and use a method that i could name instantiate from csv like that now you can see that this one is also going to receive self because if you remember i said that in each method that we will design we need to receive at least one parameter that will be passed as the instance itself because this is how python oop works now the problem is we are not going to have any instances on our hand to call this method from the instance because this method is actually designed for instantiating the object itself so this means that this method could not be called from an instance so the way that this is going to be solved is by converting this method into a class method now a class method is a method that could be accessed in the following way so i will use this line to delete that and it could be accessed from the class level only so this will look like item dot instantiate from csv and then in here we will probably pass our csv file so this method should take full responsibility to instantiate those objects for us so now that we understood this let's go ahead and see how we can create a class method so for sure we need to delete the self and i know that we have errors but we are going to solve each one of those just in a second now in order to convert this to a class method we need to use a decorator that will be responsible to convert this method into a class method now decorators in python is just a quick way to change the behavior of the functions that we will write by basically calling them just before the line that we create our function so we could use the add sign and use the class method in here and then this instan

Original Description

Object Oriented Programming is an important concept in software development. In this complete tutorial, you will learn all about OOP and how to implement it using Python. 💻 Code: https://github.com/jimdevops19/PythonOOP 🔗 Tutorial referenced for a deeper explanation about __repr__: https://www.youtube.com/watch?v=FIaPZXaePhw ✏️ Course developed by Jim from JimShapedCoding. Check out his channel: https://www.youtube.com/channel/UCU8d7rcShA7MGuDyYH1aWGg ❤️ Try interactive Python courses we love, right in your browser: https://scrimba.com/freeCodeCamp-Python (Made possible by a grant from our friends at Scrimba) ⭐️ Course Contents ⭐️ ⌨️ (0:00:00) Getting Started with Classes ⌨️ (0:12:11) Constructor, __init__ ⌨️ (0:50:35) Class vs Static Methods ⌨️ (1:13:22) Inheritance ⌨️ (1:30:14) Getters and Setters ⌨️ (1:51:00) OOP Principles 🎉 Thanks to our Champion and Sponsor supporters: 👾 Wong Voon jinq 👾 hexploitation 👾 Katia Moran 👾 BlckPhantom 👾 Nick Raker 👾 Otis Morgan 👾 DeezMaster 👾 AppWrite -- Learn to code for free and get a developer job: https://www.freecodecamp.org Read hundreds of articles on programming: https://freecodecamp.org/news
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from freeCodeCamp.org · freeCodeCamp.org · 0 of 60

← Previous Next →
1 React: Production Server Setup Part 2 - Live Coding with Jesse
React: Production Server Setup Part 2 - Live Coding with Jesse
freeCodeCamp.org
2 cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
freeCodeCamp.org
3 Browser history tutorial - Beau teaches JavaScript
Browser history tutorial - Beau teaches JavaScript
freeCodeCamp.org
4 Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
freeCodeCamp.org
5 React: Parameterized Routing with Next.js - Live Coding with Jesse
React: Parameterized Routing with Next.js - Live Coding with Jesse
freeCodeCamp.org
6 React: Dealing with jQuery Issues - Live Coding with Jesse
React: Dealing with jQuery Issues - Live Coding with Jesse
freeCodeCamp.org
7 setInterval and setTimeout: timing events - Beau teaches JavaScript
setInterval and setTimeout: timing events - Beau teaches JavaScript
freeCodeCamp.org
8 Browser and Device Testing - Live Coding with Jesse
Browser and Device Testing - Live Coding with Jesse
freeCodeCamp.org
9 Last Minute Updates - Live Coding with Jesse
Last Minute Updates - Live Coding with Jesse
freeCodeCamp.org
10 Post Launch Updates - Live Coding with Jesse
Post Launch Updates - Live Coding with Jesse
freeCodeCamp.org
11 React: Setting Up Google Analytics - Live Coding with Jesse
React: Setting Up Google Analytics - Live Coding with Jesse
freeCodeCamp.org
12 React: Masonry Layout - Live Coding with Jesse
React: Masonry Layout - Live Coding with Jesse
freeCodeCamp.org
13 Load Balancing Digital Ocean Droplets - Live Coding with Jesse
Load Balancing Digital Ocean Droplets - Live Coding with Jesse
freeCodeCamp.org
14 try, catch, finally, throw - error handling in JavaScript
try, catch, finally, throw - error handling in JavaScript
freeCodeCamp.org
15 Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
freeCodeCamp.org
16 Graphs: breadth-first search - Beau teaches JavaScript
Graphs: breadth-first search - Beau teaches JavaScript
freeCodeCamp.org
17 React: Masonry Layout Part 2 - Live Coding with Jesse
React: Masonry Layout Part 2 - Live Coding with Jesse
freeCodeCamp.org
18 React: WordPress API Live Search - Live Coding with Jesse
React: WordPress API Live Search - Live Coding with Jesse
freeCodeCamp.org
19 Creating WordPress Custom Post Types - Live Coding With Jesse
Creating WordPress Custom Post Types - Live Coding With Jesse
freeCodeCamp.org
20 Dates - Beau teaches JavaScript
Dates - Beau teaches JavaScript
freeCodeCamp.org
21 Miscellaneous Front End Updates - Live Coding with Jesse
Miscellaneous Front End Updates - Live Coding with Jesse
freeCodeCamp.org
22 Merging a Pull Request from GitHub - Live Coding with Jesse
Merging a Pull Request from GitHub - Live Coding with Jesse
freeCodeCamp.org
23 React + Prettier + Standard JS - Live Coding with Jesse
React + Prettier + Standard JS - Live Coding with Jesse
freeCodeCamp.org
24 React: Sortable Responsive Table - Live Coding with Jesse
React: Sortable Responsive Table - Live Coding with Jesse
freeCodeCamp.org
25 Geolocation Sorting by Distance - Live Coding with Jesse
Geolocation Sorting by Distance - Live Coding with Jesse
freeCodeCamp.org
26 Tradeoff Matrix - Agile Software Development
Tradeoff Matrix - Agile Software Development
freeCodeCamp.org
27 The Definition of Ready - Agile Software Development
The Definition of Ready - Agile Software Development
freeCodeCamp.org
28 Getting first React job without experience - Ask Preethi
Getting first React job without experience - Ask Preethi
freeCodeCamp.org
29 React: Google Analytics Click Tracking - Live Coding with Jesse
React: Google Analytics Click Tracking - Live Coding with Jesse
freeCodeCamp.org
30 Submitting a PR to an Open Source Project - Live Coding with Jesse
Submitting a PR to an Open Source Project - Live Coding with Jesse
freeCodeCamp.org
31 Should I go back to school to get CS degree? - Ask Preethi
Should I go back to school to get CS degree? - Ask Preethi
freeCodeCamp.org
32 Hero Section CSS Changes - Live Coding with Jesse
Hero Section CSS Changes - Live Coding with Jesse
freeCodeCamp.org
33 Working Agreement - Agile Software Development
Working Agreement - Agile Software Development
freeCodeCamp.org
34 A day at Pennybox with Co-Founder Reji Eapen
A day at Pennybox with Co-Founder Reji Eapen
freeCodeCamp.org
35 React: Sorting and Filtering Data - Live Coding with Jesse
React: Sorting and Filtering Data - Live Coding with Jesse
freeCodeCamp.org
36 React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
freeCodeCamp.org
37 React: Building a New UI - Live Coding with Jesse
React: Building a New UI - Live Coding with Jesse
freeCodeCamp.org
38 Definition of Done - Agile Software Development
Definition of Done - Agile Software Development
freeCodeCamp.org
39 Getting started with jQuery (tutorial) - Beau teaches JavaScript
Getting started with jQuery (tutorial) - Beau teaches JavaScript
freeCodeCamp.org
40 Making a React Blog with WordPress Content - Live Coding with Jesse
Making a React Blog with WordPress Content - Live Coding with Jesse
freeCodeCamp.org
41 React, NextJS, CSS - Live Coding with Jesse
React, NextJS, CSS - Live Coding with Jesse
freeCodeCamp.org
42 jQuery events - Beau teaches JavaScript
jQuery events - Beau teaches JavaScript
freeCodeCamp.org
43 React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
freeCodeCamp.org
44 React: Working with API Data - Live Coding with Jesse
React: Working with API Data - Live Coding with Jesse
freeCodeCamp.org
45 React: Refactoring Components - Live Streaming with Jesse
React: Refactoring Components - Live Streaming with Jesse
freeCodeCamp.org
46 jQuery effects - Beau teaches JavaScript
jQuery effects - Beau teaches JavaScript
freeCodeCamp.org
47 More React Refactoring - Live Coding with Jesse
More React Refactoring - Live Coding with Jesse
freeCodeCamp.org
48 animate in jQuery - Beau teaches JavaScript
animate in jQuery - Beau teaches JavaScript
freeCodeCamp.org
49 "Finishing" My React Site - Live Coding with Jesse
"Finishing" My React Site - Live Coding with Jesse
freeCodeCamp.org
50 Starting a New React Project (P2D1) - Live Coding with Jesse
Starting a New React Project (P2D1) - Live Coding with Jesse
freeCodeCamp.org
51 React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
freeCodeCamp.org
52 The Agile Manifesto - Agile Software Development
The Agile Manifesto - Agile Software Development
freeCodeCamp.org
53 jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
freeCodeCamp.org
54 React Project 2 Day 3 - Live Coding with Jesse
React Project 2 Day 3 - Live Coding with Jesse
freeCodeCamp.org
55 The INVEST approach to product backlog items
The INVEST approach to product backlog items
freeCodeCamp.org
56 React Project 2 Day 4 - Live Coding with Jesse
React Project 2 Day 4 - Live Coding with Jesse
freeCodeCamp.org
57 Chickens and Pigs - Agile Software Development
Chickens and Pigs - Agile Software Development
freeCodeCamp.org
58 React Project 2 Day 5 - Live Coding with Jesse
React Project 2 Day 5 - Live Coding with Jesse
freeCodeCamp.org
59 jQuery: add and remove DOM elements - Beau teaches JavaScript
jQuery: add and remove DOM elements - Beau teaches JavaScript
freeCodeCamp.org
60 React Project 2 Day 6 - Live Coding with Jesse
React Project 2 Day 6 - Live Coding with Jesse
freeCodeCamp.org

This video tutorial teaches Object Oriented Programming with Python, covering key concepts such as classes, constructors, and inheritance. By the end of the tutorial, viewers will be able to write object-oriented Python code and apply OOP principles in software development. The tutorial is designed for beginners and provides a comprehensive introduction to OOP with Python.

Key Takeaways
  1. Create a class in Python
  2. Define a constructor using __init__
  3. Implement class and static methods
  4. Use inheritance to create a subclass
  5. Define getters and setters for attributes
  6. Apply OOP principles in software development
💡 Object Oriented Programming is a fundamental concept in software development, and Python provides a simple and intuitive way to implement OOP principles.

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →