Python Tutorial for Beginners 25 - Python __init__ and self in class

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to the next video on Python tutorial for beginners in this video I'm going to show you how to use init method in Python and how to use a keyboard 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 passed 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 in it 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 am 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 in it 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 in it 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++ 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 collections 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 control 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 ctrl + /o K 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 in it missing two 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 two 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 or 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++ or Java if you are 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 itself once again I can use self to set the value of color here so self dot 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 self now it's not necessary to provide this same kevo which itself 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 at last 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 am 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 self 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 the 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 that's it for this video I will see you in the next video
Info
Channel: ProgrammingKnowledge
Views: 42,901
Rating: 4.784431 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: oE_dWWWqxQ0
Channel Id: undefined
Length: 12min 5sec (725 seconds)
Published: Thu Sep 13 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.