Python Self
Learn via video course
Overview
In Python, the self parameter refers to the instance of a class, allowing access to its members and methods, as well as facilitating the initialization of new members. self acts as a reference to the instance itself and is automatically passed by Python when calling object methods, and we can use another name for the parameter instead of self.
Introduction
Suppose you have a class named as a student with three instance variables student_name, age, and marks. Now you want to access these members in a class function. What should you do? You cannot call these members directly. This will cause an error because Python will search these variables outside the class instead of the class. Now how can we solve this problem? The answer is using a self word (a reference to class). We pass the self as an argument in the method and access these members, or also we can manipulate the state of these members. Let's dive deep and understand some characteristics or properties of the self.
What is Self in Python?
The self is an instance of the class; by using the self, we can access or manipulate the state of the instance members of the class. Also, we can create new instance members with the help of self. The self is mostly used for initializing the instance members of the class.
Syntax
The self is passed as an argument in the method of the class, and by default, it is compulsory to pass the reference of the class in all the methods of the class.
Code:
Output:
In the above program, we pass the self in the method and access the class instance variable, i.e., a.
What is the Use of Self in Python?
Python doesn’t provide any operator for accessing the instance members of the user-defined class but allows the user to pass the instance of a class, i.e., self, as an argument to the method so that the user can access the instance variables.
Code:
Output:
In the above program, we are accessing the instance variable with the help of the self word and without using the self.
Why Self is Defined Explicitly in Python?
According to the zen of Python "Explicit is better than implicit". Because explicitly writing the code(clearly defining the state of something even if it is obvious) helps to increase the readability of the code. In a Python programming language, we have to pass the reference of the class (using the self word) as an argument in every method of the class, but why do we have to send the class reference, i.e., self, to all the methods? Can we use the self as a parameter?
Code:
Output:
In the above example, when we call the method of the class Scaler using an object, i.e.a.hello(1), Internally during the calling of this method, the Python language converts the calling to class.hello(a(object), number). This is why we have to first argument as a reference (self word) in the method, and also, this is the reason self is defined explicitly.
Python Class self Constructor
A class constructor is a special method that is automatically called when an object of a class is created. The constructor method is defined using the __init__() function within a class.
The self parameter in the constructor represents the instance of the object being created, just like in any other method. The self parameter allows you to access and manipulate the attributes and methods of the object within the class.
Here's an example of a Python class with a constructor:
Output:
The code demonstrates a Python class called MyClass with two methods: a constructor (__init__) and a display method (display_info).
- The constructor initializes the object's attributes using the provided name and age parameters.
- An instance of MyClass is created with the arguments "John" and 25, and Python automatically handles the self parameter.
- The display_info method is called, printing the stored name and age attributes.
Is Self in Python a Keyword?
The self word is not a keyword in Python. For the sake easiest naming convention, we often use the self word in place of the other word, and it is advisable to use the self word instead of the other word because one of the major reasons for this is most of the internal Python libraries used word self to represent the reference of the object. So to reduce the conflict between the programmers and the inbuilt libraries, we often use the self word.
Code:
Output:
In the above program, we used another word instead of self to represent the object state, i.e., hello, which proves self is not a keyword; we can use any other name according to our choice :).
How Can We Skip Self in Python?
Now, Suppose you want to skip the self word as a parameter in the class methods. What should you do? We can skip self as an argument in the method by adding the @staticmethod decorator on the method, which makes the method static. The static methods don't need the reference of the class. A static method cannot access or modify the class members. We generally use static methods when we write some operations that are not supposed to be changed in the future, like some fixed arithmetic calculations.
Code:
Output:
Conclusion
- Self is used for accessing the instance members of the class.
- Self is not a keyword in Python.
- We have to pass self as a parameter in every class method by default.
- We can skip the self as a parameter in a method by using the @staticmethod decorator on the method of the class.
- Self is always defined explicitly.
The most common application of the self is it helps to call the instance member or method in any method. For example, we can call any method in the body of another method any number of times with our help of self.