self represents the instance of the class. By using the “self” keyword we can access the attributes and methods of the class in python. It binds the attributes with the given arguments.
The reason you need to use self. is because Python does not use the @ syntax to refer to instance attributes. Python decided to do methods in a way that makes the instance to which the method belongs be passed automatically, but not received automatically: the first parameter of methods is the instance the method is called on.
In more clear way you can say that SELF has following Characteristic-
Self is always pointing to Current Object.
Address of self = 140124194801032 Address of class object = 140124194801032
Another Example of Using SELF:
Model is audi a4 color is blue Model is ferrari 488 color is green
Self is the first argument to be passed in Constructor and Instance Method.
Self must be provided as a First parameter to the Instance method and constructor. If you don’t provide it, it will cause an error.
Self is a convention and not a Python keyword .
self is parameter in Instance Method and user can use another parameter name in place of it. But it is advisable to use self because it increases the readability of code, and it is also a good programming practice.
we have used another parameter name in place of self
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course