Python OOP Tutorials | Python __init__, Constructors and Self

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'm going to show you how to use init method in python and how to use a keyword called self in python so i will continue with the class which i have created in the last video and this was the class which we have created and we have used this special keyword pass there and pass we were using to create an empty class now i'm going to remove this pass and i'm going to add a method called init here so i'm going to just write def and then underscore underscore init and then press enter and you can see this init method is created here so this is like a normal method with double underscore in front and back of this init keyword and you will also see the self keyword is already added as the first argument of this method now this init method serves as a constructor for the class so usually it is used to initialize some attributes or some functions because this is the first method which will be called when you create an instance of a class so here we are creating an instance of a class and this init method will be the first method which will be called when this object or instance will be created so let's check what i'm saying so let's say i want to print inside this init method that the underscore underscore init underscore underscore is called and let me run this code so i will change this to car and then run this code and now when i see this result you will see this line is called three times because we have created three instance from the same class okay so every time this instance is created this print is called and that's why this line is printed three times and everything else will be printed after that whatever we were printing for example speed or color of the car will be printed after that now one important thing to notice here is i said init serves as a constructor it's not a constructor although it would be tempting to call this init method as a constructor actually it's not a constructor but it behaves like a constructor because init is the closest thing we are going to get in python to a constructor because it is the first method which is called whenever an instance is created now if you are familiar with other object oriented programming languages like java and c plus plus there is a destructor also in those kind of languages with classes now python doesn't have any destructor because python has an automatic garbage collection so you don't need a destructor in python because python will take care of anything which should be taken care of now as i said usually init method is used to initialize something so instead of initializing the value of speed and initializing the value of color let me just comment this code first of all so i'm going to select whatever i want to comment and then i can press ctrl forward slash to comment all the line you can also go to code and then use this option which says comment with line comment and you can see the shortcut for that control plus forward slash okay so this is going to comment your lines of code and now i want to use the speed as the initialization value so after the self keyword i can give the next parameter which is speed here and then the third parameter is the color here and now to print the value of speed and color i can use once again print and then first of all i'm going to print the speed and then i'm going to print the color attribute and as soon as you do this and when you try to run this program it will give you an error so let me run the program and it will give me the error it says init missing to required positional argument which is speed and color okay so once you create an init method and provide any arguments other than self self is automatically provided by python whenever you create an instance of a class but other than self when you write for example speed and color arguments here you need to provide those argument at the initialization of your class so here first we will give the speed inside these parentheses for example 200 and then the color for example red here okay same we need to do for the other 2 instantiation of the car class so let me do it for the second instance and also for the third instance and now let's run the code once again and let's see what happens so now you can see everything works fine and no error is given to us and you will also see because this print is called first you can see the speed is printed first and then the color and then this line is printed after that and that means we will get the speed color and this line three times for every instance with different values whatever values you have provided for the instantiation of your car class now usually you provide these arguments because you want to initialize the value of speed so let's try to access the value of the ford object speed and color so let me just uncomment this code and let's run this code and it will give us an error you can see this says that car object has no attribute called speed right so what is the error because we have provided these attribute speed and color but we haven't assigned these values to any attribute inside this car class earlier what we have done is we have assigned the speed value to a speed attribute and the color value to the color attribute but we have already commented those codes so how can we assign the speed and color to the car object so it turns out that you can use the self keyword and then using the self keyword you can assign the value to the current object so self is essentially the current object okay it's similar to using this in c plus plus or java if you're familiar with those two languages so you use self dot and then the name of the attribute for example speed in our case is equal to whatever argument you provide for speed so we have provided the same argument which is speed is itself once again i can use self to set the value of color here so self.color is equal to color let me remove this semicolon because it's not required and now when i run this code you will see that there is no error now so because we have now used the self keyword to set the attributes of speed and color so we can easily access the values of the speed and color using any object of the car class so let me once again minimize this so now let's talk about the self keyword here so whenever you create a class the first argument of every method you need to provide this keyword yourself now it's not necessary to provide this same keyword which is self but it's a convention to write this self as the first parameter the first parameter can be for example abc it doesn't matter but you need to use this abc here also as self and it will be totally fine but it's a convention that we use the self keyword in order to indicate that this is the current object so every method you will create you need to give this self keyword as the first argument of your method inside a class now you may also observe that here when i am initializing this class instance i'm not providing any self keyword so even though in the init method i have provided three arguments i'm only providing two arguments here so it turns out that you don't need to provide the first argument which is self it will be automatically be provided to your class so you just need to give the next argument whatever argument you give after the cell so we have given speed and color after the self argument so we just need to provide those arguments after whatever you use after this keyword called self so let's do the same thing with our second class which is the rectangle class and here also we have created this empty class so let's remove this pass keyword and instead of this pass keyword we will use this init method which is def underscore underscore init underscore underscore and here after the self we will provide the height as the second argument and the width as the third argument okay and then we are going to initialize the attributes height and width using this self keyword so self dot height is equal to height self dot width is equal to width okay so this is how you can initialize your attribute using this init method and now when you do this you don't need to initialize these values like this you can directly initialize this height and width using these parentheses let me provide these values let's say 20 and 60 for the first rectangle and let's say 50 and 40 for the second rectangle and when we run this code let me just change the file here and then run the code and you will see it will print the area once again here so this init method is used to initialize your attributes or whatever you want to initialize at the start of your class you will do all those initializations inside this init method so this is how you can use init method and self keyword in python we will discuss more about init method and the self keyword so let's get started so here i have a very simple class called hello and in this class i have a method called init and i'm just instantiating this class and creating an object out of this hello class now the first question which may arise is what if i don't want to use this self keyword you can see directly that there is a red squiggly line appears here which means an error and this we can also see when we run the program so when we run the program it's going to give us this error which says init takes zero arguments but given one now you may ask why it's saying that we have given one argument to the initialization of this hello class so as i said in the last video that self is automatically passed when you initialize a class and that's why it says that one argument is given but init takes zero argument because here we haven't given any argument inside these parentheses so self is absolutely important so you need to give the self now the second question you may ask is can i create multiple init method inside a class so let's see if it's possible or not so here i'm going to give this pass keyword which means that this init method is an empty method and let me create a second init method and this time i want to add one more parameter here let's say name okay so we have provided two init methods here and let's see what happens so this is the instantiation of class where i haven't given any argument to this instantiation so i'm going to run the program and what it says it says init missing one required positional argument which is name so it turns out that it's not possible to provide multiple init methods in your python class if you provide multiple init method in your python class the init method which you define at last will be considered as the main init method and other will be overwritten okay so whatever init method you define at last that signature of init method will be valid and all the other init method which you will create will be overwritten by the last init method okay so let's provide the argument name here so i'm going to just provide one argument here and then run this code and it works perfectly fine okay let me just reverse this order so instead of using this init at the top i will use the init which doesn't take any parameter at the bottom now okay and we provide this argument and let's run the code and now once again we get the error which says init takes one positional argument but two were provided so one positional argument means that this init only takes the self argument and nothing else but here self is provided automatically but we are providing an extra parameter here which is not required and that means this init which we have defined later is valid and this previous init is not valid so always remember it's not allowed to use multiple init method in the python class but if you do this the last init method will be a valid init method and all the other init method will be the invalid init method now one more question you may ask here what if i want to create both kind of instances one which takes no argument here and other which takes one argument or multiple arguments here so the answer is in my previous video in which i have shown you how to provide default value to your arguments so let's say i provide a default value to my argument here now i will create two instances of the same class one takes no argument and other takes one argument and when i run this code no error appears here okay so if you want to create a init method with multiple parameters you can either use this default value for your init method parameters or what you can do here is you can use this parameter which takes a tuple so in the previous video we have seen that we can provide an argument with asterisks in front of it and this means that we can provide multiple parameters to our method here and when we run the code it's also valid we can also provide multiple parameters here and it will also be totally valid so i'm going to run the program once again and you can see it's totally valid so if you want to provide multiple parameters to your init method you can use this kind of notation or you can provide the default value to your parameter or the last thing you can use is you can use for example this type of argument which takes the keyword so kw args here okay and this also we have seen whenever you use this kind of notation that means you want to provide the dictionary right key value pair so let me provide some kind of key value pair let us say name is equal to some kind of name here and then let me run the code and it's totally valid it doesn't give us any error so even though multiple init methods are not allowed in python you can use these type of notations in order to give variable length argument in your init method now let me just remove all the parameters from here and this instantiation from here also and let's take the next question so let's say i want to remove this pass keyword from here and here i want to initialize some values so self dot name is equal to let's say we will provide some name so i'm going to provide a argument called name and self.name is equal to name here and then let's say self dot age here and we provide some value which is not taken from the argument but we provide some static value here is it allowed yes it's totally allowed right so if you want to provide some default value for your attribute you can absolutely provide that without even passing it as an argument so it's not necessary that all the attribute values you need to provide from this argument list you can provide any default or static value here which is not coming as an argument so i hope that clarifies some more details about this init method thanks for watching i will see you in the next video
Info
Channel: ProgrammingKnowledge
Views: 2,862
Rating: 4.909091 out of 5
Keywords: Windows 10, Python (Programming Language), Python 3.6, Python, Install Python, Download, Python 3.x.x, Programming Language (Software Genre), Python Tutorial, Python Tutorial for Beginners, Absolute Beginners, Python for Beginners, Python course, python tutorial, python scripting tutorial, Online Course, Python Guru, Learn Python, python 3.7, Python 3, Functions in Python, Python Class, Python Class examples, python 3 class, __init__, self, Python __init__
Id: yoqTQs9CBXI
Channel Id: undefined
Length: 19min 54sec (1194 seconds)
Published: Sat Jun 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.