self in Python

Last Updated : 4 Jul, 2026

self parameter refers to the current object of a class. It helps Python identify the object on which a method is called, allowing each object to access and maintain its own data independently.

  • self is not a keyword, but a naming convention
  • When you call a method using an object, Python automatically passes that object as the first argument to the method.
  • When defining methods inside a class, the first parameter is always self.

Code shows how self is used to store data in an object and access it through methods

Python
class Car:
    def __init__(self, brand, model):
        self.brand = brand  # Set instance attribute
        self.model = model  # Set instance attribute

    def display(self):
        return self.brand, self.model

# Create an instance of Car
car1 = Car("Toyota", "Corolla")

# Call the display method
print(car1.display())

Output
('Toyota', 'Corolla')

Explanation:

  • self in __init__ assigns values (brand and model) to the specific instance (car1).
  • self in display refers to the same instance (car1) to access attributes.
  • Python automatically passes car1 as the first argument to display().

Self as Default Argument

The main reason Python uses self is to make object-oriented programming explicit rather than implicit. By requiring the instance of the class as the first parameter.

  • Unlike some other programming languages, Python doesn’t hide this reference automatically.
  • This makes it clear and unambiguous that the method is operating on an instance of the class.
  • It improves readability and avoids confusion (especially in inheritance).

Below example shows how self is used to access attributes of the correct instance while performing calculations.

Python
class Circle:
    def __init__(self, r):
        self.r = r

    def area(self):
        a = 3.14 * self.r ** 2
        return a

# Creating an instance of Circle
ins = Circle(5)

# Calling the area method
print("Area of the circle:", ins.area())

Output
Area of the circle: 78.5

Explanation:

  • self.r = r assigns 5 as the radius of the circle instance ins.
  • Inside area(), self.r ensures radius belongs to that specific circle (Python automatically provides self).
  • This way, even if we create multiple circles with different radii, each object uses its own value of self.r.

self in Constructors (__init__ Method)

__init__ method is automatically called when an object is created. The self parameter represents the newly created object and is used to initialize its attributes.

Python
class Subject:

    def __init__(self, sub1, sub2):
        self.sub1 = sub1
        self.sub2 = sub2

obj = Subject("Maths", "Science")
print(obj.sub1)
print(obj.sub2)

Output
Maths
Science

Explanation:

  • self.sub1 = sub1 creates an attribute named sub1 for the object.
  • self.sub2 = sub2 creates an attribute named sub2 for the object.
  • The values passed during object creation are stored in the object's attributes.

self in Instance Methods

Instance methods use self to access the attributes and other methods of the object that calls the method.

Python
class Car:
    def __init__(self, model, color):
        self.model = model
        self.color = color

    def show(self):
        print("Model:", self.model)
        print("Color:", self.color)

audi = Car("Audi A4", "Blue")
ferrari = Car("Ferrari 488", "Green")

audi.show()
ferrari.show()

Output
Model: Audi A4
Color: Blue
Model: Ferrari 488
Color: Green

Explanation:

  • self.model and self.color access the attributes of the current object. When audi.show() is called, self refers to audi.
  • When ferrari.show() is called, self refers to ferrari. This allows each object to work with its own data.

self is Not a Keyword

Yes, self is not a Python keyword. It is simply a naming convention that Python developers follow to represent the current object. Although you can use any valid parameter name, using self makes the code easier to read and understand.

Python
class Car:
    def __init__(this, model, color):
        this.model = model
        this.color = color

    def show(this):
        print("Model:", this.model)
        print("Color:", this.color)

audi = Car("Audi A4", "Blue")
audi.show()

Output
Model: Audi A4
Color: Blue

Explanation:

  • Here, this is used instead of self. The program works correctly because Python does not require the parameter name to be self.
  • However, using self is recommended because it is the standard convention.

self Refers to the Current Object

self parameter refers to the object that calls a method. It allows Python to identify and work with the attributes and methods of that specific object.

Python
class Check:
    def __init__(self):
        print("Address of self:", id(self))

obj = Check()
print("Address of object:", id(obj))

Output
Address of self: 140737343039744
Address of object: 140737343039744

Explanation:

  • id(self) returns the memory address of the current object.
  • id(obj) returns the memory address of the object created.
  • Both addresses are the same, showing that self refers to the object that invoked the method.

Using self to Modify Object Attributes

self parameter allows methods to access and update an object's attributes. This enables an object to maintain and modify its own state throughout its lifetime.

Python
class Counter:
    def __init__(self):
        self.count = 0

    def increment(self):
        self.count += 1

    def decrement(self):
        self.count -= 1

    def get_count(self):
        return self.count

counter = Counter()
counter.increment()
counter.increment()
counter.decrement()
print(counter.get_count())

Output
1

Explanation:

  • self.count stores the current count value and increment() increases the value by 1.
  • decrement() decreases the value by 1 and get_count() returns the current value of count.
  • After two increments and one decrement, the final value becomes 1.

Note: To modify an object's state, update its attributes using self.attribute. Assigning a new value directly to self does not change the original object.

Comment