What is the use of self in Python?

self is the first parameter of a method in a class. It refers to the current object, so the method can reach the attributes and other methods of that object. Python passes it automatically when you call the method.

class Student:
    def __init__(self, name):
        self.name = name

    def greet(self):
        print("Hi, I am", self.name)

s = Student("Robin")
s.greet()

Output:

Tutorials Class - Output Window

Hi, I am Robin

self is not a keyword. You could use another name, but everyone uses self, so please do the same. Learn more in Python Classes and Objects.


Learn more about the similar topics:
Tutorials
Python OOPS – Introduction
Python Classes and Objects
Python Object Methods
Python Inheritance
Polymorphism in Python
Python Access Modifiers
Exercises & Assignments
No Content Found.
Interview Questions & Answers
What is the use of self in Python?
What are public, protected and private attributes in Python?
What is the __init__() method in Python?
What is inheritance in Python?