self in python class
In Previous Chapter we found "self" inside class. We will describe here, what it is. Let's understand "class" , "self" in python more from below list gathered from various sources.
- "self" is the instance object automatically passed to the class instance's method when called , to identify the instance that called it.
- "self" does not need to be used when calling a method when you already have an available instance.
- The reason "self" is there (by convention) is that when the Python runtime sees a call of the form Object.Method(Param1,Param2), it calls Method with parameters (Object,Param1,Param2). That is why, first parameter is "self".
- object is the first argument to the function, which traditionally is named self in the parameter list. (You can, in fact, name it something else, and your code will work correctly, but other Python coders will frown at you.)
- Classes are more than that in Python. Classes are objects too.
- As soon as you use the keyword
class, Python executes it and creates an OBJECT.
- self is not a reserved keyword in python it’s just a strong convention. Many people say that why do we have to write self ? Why can’t we have it set automatically like in Java ? Someone also filed a PEP (improvement suggestion) in which he suggested to remove the explicit assignment of self keyword. However Guido Van Rossum (the maker of python) wrote a blogpost in which he told why explicit self has to stay