Why Python Uses 'Self' as Default Argument
Last Updated :
20 Sep, 2025
In Python, when defining methods inside a class, first parameter is always self. It is not a keyword, but a naming convention that plays a key role in Python’s object-oriented programming. The self parameter represents instance of the class itself, allowing you to access and modify its attributes and methods.
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().
Now, Let's understand why python uses self.
Why use Self?
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, Python ensures:
- Clarity: Following Python’s “explicit is better than implicit” rule, it makes code easier to read.
- Flexibility: You can use any name (like this), but self is the standard.
- Consistency: Keeps all methods uniform across classes.
Why Not Implicit?
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, which improves readability and avoids confusion (especially in inheritance).
Object Initialization & Method Invocation
In this example, self is used to initialize an object’s topic and access it inside a method.
Python
class gfg:
def __init__(self, topic):
self._topic = topic # Store parameter value in instance variable
def topic(self):
print("Topic:", self._topic) # Access the renamed variable
# Creating an instance of gfg
ins = gfg("Python")
# Calling the topic method
ins.topic()
Explanation:
- self._topic = topic assigns "Python" to the instance ins.
- Inside topic(), self._topic refers to that stored value (Python automatically provides self when you call ins.topic()).
Circle Class for Area Calculation
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())
OutputArea 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.
Suggested Quiz
6 Questions
What is the purpose of the self keyword in Python classes?
-
It refers to the class itself
-
It is used to define private methods
-
It refers to the current instance of the class
-
It refers to the superclass
Explanation:
self represents the instance of the class. It's used to access instance variables and methods from within the class.
What will be the output of the following code?
class MyClass:
def __init__(self, value):
self.value = value
def get(self):
return self.value
obj = MyClass(10)
print(obj.get())
Explanation:
The constructor initializes self.value with 10, and get() returns that value.
Why must self be the first parameter in instance methods?
-
It's required by the Python interpreter to distinguish instance methods
-
It's used for memory management
-
It's a syntax convention, not a requirement
-
It's only needed when inheriting from another class
Explanation:
The Python interpreter automatically passes the instance (self) as the first argument when calling instance methods.
What will happen if self is omitted from an instance method definition?
class Test:
def show():
print("Hello")
t = Test()
t.show()
Explanation:
The method show() doesn't accept self, so when the instance t is used to call it, Python still passes self, causing a TypeError.
What will be the output of the following code?
class Sample:
def __init__(this, x):
this.x = x
def show(this):
print(this.x)
s = Sample(5)
s.show()
Explanation:
Although this is used instead of self, it's just a parameter name. The code works and prints 5.
What does self.__class__ refer to in an instance method?
-
The class of the self instance
-
-
-
The memory location of self
Explanation:
self.__class__ refers to the class to which the instance belongs, useful for introspection or logging.
Quiz Completed Successfully
Your Score : 2/6
Accuracy : 0%
Login to View Explanation
1/6
1/6
< Previous
Next >
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice