Self in Python Class

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

In Python, self serves as a reference to the current class instance, enabling access and modification of its attributes and methods. When defining class methods, it is crucial to ensure Python knows to look within the class scope for attributes like student_name, age, and marks. Although self is a convention, any other name can technically be used, but adhering to this standard promotes readability and consistency in code. Python seamlessly links method calls to their class instance through self, maintaining object-oriented integrity.

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.

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 methods. The self parameter allows you to access and manipulate the attributes and methods of the object within the class.

Self: Pointer to Current Object

In Python classes, self serves as a reference to the current instance of the class. Let's illustrate this with a simple example:

In this example, self refers to the current instance (obj1 or obj2) when display() method is called. It allows access to the value attribute specific to each object, ensuring that the correct value is displayed for each instance.

Self in Constructors and Methods

In constructors (__init__ methods) and other class methods, self is used to reference the instance on which the method is being called. Here's an example:

In this example, self.name and self.age within the __init__ method initialize the name and age attributes for the current instance (person1). Similarly, within the display_info method, self.name and self.age allow access to the attributes specific to person1, displaying its name and age.

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 static method in python 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.

See Also