Home » 
        Python
    
    Why Python Uses 'Self' as Default Argument
    
    
    
    
        
        Last Updated : May 03, 2025
        
    
In Python, the keyword self is used as the first parameter of instance methods to represent the instance of the class. It allows access to the attributes and methods of the class. In this chapter, we will understand why Python uses self and how it works with the help of examples.
What is self in Python?
self refers to the current instance of the class. It is automatically passed to instance methods when they are called on an object. Using self, you can access and modify the object's properties from within its methods.
Example
In this example, we define a Student class and use self to access instance variables.
class Student:
    def __init__(self, name, city):
        self.name = name
        self.city = city
    def show_details(self):
        print(f"Name: {self.name}, City: {self.city}")
# Creating object
student1 = Student("Amit", "Delhi")
# Calling method
student1.show_details()
The output of the above code would be:
Name: Amit, City: Delhi
Why is self Needed?
Python does not use the this keyword like some other programming languages. It uses self explicitly to make it clear that the method belongs to the instance and not the class. This helps avoid confusion.
Example
In this example, self helps differentiate between instance variables and method parameters.
class Employee:
    def __init__(self, name):
        self.name = name  # self.name is the instance variable
    def greet(self):
        print(f"Hello, my name is {self.name}")
# Creating object
emp1 = Employee("Priya")
emp1.greet()
The output of the above code would be:
Hello, my name is Priya
self is Just a Convention
The word self is not a keyword in Python. You can use any valid variable name in its place, but using self is a widely accepted convention.
Example
In this example, we use a different name instead of self, but it still works the same way.
class Car:
    def __init__(this, brand):
        this.brand = brand
    def show_brand(this):
        print(f"Brand: {this.brand}")
# Creating object
car1 = Car("Mahindra")
car1.show_brand()
The output of the above code would be:
Brand: Mahindra
Accessing Other Methods with self
You can also call other methods of the same class using self.
Example
In this example, we call one instance method from another using self.
class Teacher:
    def __init__(self, name):
        self.name = name
    def display(self):
        print(f"Teacher's name is {self.name}")
    def welcome(self):
        print("Welcome!")
        self.display()
# Creating object
teacher1 = Teacher("Suresh")
teacher1.welcome()
The output of the above code would be:
Welcome!
Teacher's name is Suresh
Exercise
Select the correct option to complete each statement about the use of self in Python methods.
    - The self keyword in a class method refers to the ___.
        
    
 
    - Using self allows access to ___ within the class.
        
    
 
    - In Python, the self parameter ___ be passed explicitly when calling a method on an object.
        
    
 
    
    
  
    Advertisement
    
    
    
  
  
    Advertisement