The explicit 'self' in a Python class.
British people be like: It’s an initializer, __innit__?
In Python, whenever we create a function inside a class i.e. a method, we used the ‘self’ keyword as the first parameter in each case.
A quick look at _innnit_ first:
#Creating the constructor method inside the class Person
def __innit__(self, name, age, profession):
self.age = age
self.name = name
self.profession = profession
# Creating Objects
Person1 = Person(“Saul Goodman”, 45, “lawyer”)
Person2 = Person(“Kim Wexler”, 40, “lawyer”)
Here, we initalize the objects using __innit__ and define the variables, later we create the two instances of the class i.e. the objects - ‘Person1’ and ‘Person2’.
The arguments of the objects are according to the number of parameters in the constructor. In our case, apart from self, there are three parameters in our constructor - name, age and profession hence we give - “Saul Goodman”, 45, “lawyer” as the arguments to the object.
In conclusion, __innit__ will act like a blueprint for the class ‘Person’.
About the ‘self’ keyword
In the example below let’s create a Person who has a name, age and profession.
class Person
def __innit__(self, name, age, profession):
self.age = age #Add a variable that has the same value as 'age'.
self.name = name
self.profession = profession
def printThis(self):
print(Hey! I am “{self.name}“, I am a “{self.profession}”)
Person1 = Person(“Saul Goodman”, 45, “lawyer”)
#Output:
>>> Hey! I am Saul Goodman, I am a lawyer
Here, in the constructor, if ‘self’ was absent we could have used the three parameters only within the __innit__ method. When we attach it to ‘self’, we can use it all over the class, hence, we can use the variable ‘name’ from the __innit__ method in the printThis() method.
So, ‘self’ is the class itself. Whenever we refer to ‘self’ we are referring to the variables in the class. Example - when we say self.name we are actually referring to class.name
In our example, self.name and name are NOT the same! Instance attributes are defined in the constructor, ’self.name’ is the instance attribute whereas name is the local variable.
About the __call__ function
We can also use the __call__ function that will create an in-built function inside the class, so we don’t need to create a function ‘printThis()’ of our own.
class Person
def __innit__(self, name, age, profession):
self.age = age
self.name = name
self.profession = profession
def __call__(self):
print((Hey! I am “{self.name}“, I am a “{self.profession}”))
Person1 = Person(“Saul Goodman”, 45, “lawyer”)
Person1()
#Output:
>>> Hey! I am Saul Goodman, I am a lawyer
Here, instead of creating a function called printThis() we use the __call__ function. It helps us to treat object as function. In our case, the object - Person1 is treated called as a function - Person()
More about self:
Unlike ‘this’ in Java and C++, ‘self’ is NOT a keyword in python. In fact, you could replace the word self with any non-keyword name you’d use for a variable. Using ’self’ does make it easier to distinguish it from local variables.
Read more from the creator of Python: why the explicit self has to stay.