The self Parameter in Python
The self Parameter
Inside a class, every regular method receives a first parameter named self. self is a reference to the current object — the specific instance the method was called on. It is how a method reads and changes that object's own data and calls its other methods.
Understanding self is the key to understanding how objects keep their own separate data while sharing the same class code.
What Is self?
When you call a method on an object, Python automatically passes the object as the first argument:
p1.show_profile()
is translated by Python into:
Profile.show_profile(p1)
Inside the method, the parameter self receives p1. So self.username means "the username attribute of the object this method was called on".
Using self to Access Object Attributes
Attributes that belong to an object are created and accessed through self.
Example: Accessing Attributes With self
class Profile:
def __init__(self, username, points):
self.username = username
self.points = points
def show_profile(self):
print(f"User: {self.username}, Points: {self.points}")
p1 = Profile("Arjun", 120)
p1.show_profile()
Expected output:
User: Arjun, Points: 120
What happens:
Profile("Arjun", 120)creates an object and calls__init__(). Inside it,selfis the new object, soself.username = usernamestores"Arjun"on that object.p1.show_profile()calls the method, and Python passesp1asself.self.usernameandself.pointsread the values stored onp1.
Each object has its own values for these attributes.
Why self Is Necessary
A class defines methods once, but there may be many objects. Without self, a method would not know which object's data to use.
Example: One Method, Many Objects
class Student:
def __init__(self, name):
self.name = name
def display_name(self):
print(self.name)
s1 = Student("Neha")
s2 = Student("Karan")
s1.display_name()
s2.display_name()
Expected output:
Neha
Karan
The same display_name() code runs both times, but:
- In
s1.display_name(),selfiss1, so it prints"Neha". - In
s2.display_name(),selfiss2, so it prints"Karan".
What Happens Without self?
If you forget self. when creating an attribute, you create a local variable that disappears when the method ends:
class Student:
def __init__(self, name):
name = name # Missing self. — this does nothing useful
def display_name(self):
print(self.name)
s1 = Student("Neha")
s1.display_name()
Output:
AttributeError: 'Student' object has no attribute 'name'
The object never received a name attribute, because the value was only stored in a temporary local variable.
Rule: self Must Be the First Parameter
In every instance method (a regular method that works with an object), self must be the first parameter. Python passes the object into that position automatically, so you never pass it yourself when calling the method.
Forgetting self in the definition causes an error when the method is called:
class Demo:
def show():
print("Hello")
d = Demo()
d.show()
Output:
TypeError: Demo.show() takes 0 positional arguments but 1 was given
The error says "1 was given" because Python automatically passed the object d, but the method had no parameter to receive it.
self Is a Convention, Not a Keyword
self is not a reserved word in Python. The first parameter could technically have any name — Python simply passes the object into the first position. However, using self is a universal convention, and PEP 8 recommends it.
Example: Using a Different Name (Not Recommended)
class Device:
def __init__(obj, name):
obj.name = name
def show(obj):
print(obj.name)
d1 = Device("Printer")
d1.show()
Expected output:
Printer
This works, but it makes the code harder for other developers to read. Always use self.
Accessing Multiple Attributes With self
A method can use self to access any number of the object's attributes.
Example: Displaying Object Information
class Laptop:
def __init__(self, brand, ram, price):
self.brand = brand
self.ram = ram
self.price = price
def details(self):
print(f"{self.brand} | {self.ram}GB RAM | ₹{self.price}")
lap1 = Laptop("HP", 16, 65000)
lap1.details()
Expected output:
HP | 16GB RAM | ₹65000
Modifying Object Data With self
self is not only for reading data — methods also use it to change the object's attributes.
class Wallet:
def __init__(self, balance):
self.balance = balance
def add_money(self, amount):
self.balance += amount
w = Wallet(500)
w.add_money(250)
print(w.balance)
Expected output:
750
self.balance += amount updates the balance stored on w. Other Wallet objects are not affected.
Calling One Method From Another Using self
Methods in the same class can call each other through self.
Example: A Method Calling Another Method
class Greeting:
def __init__(self, name):
self.name = name
def say_hello(self):
return f"Hello {self.name}"
def full_message(self):
print(self.say_hello() + ", have a great day!")
g1 = Greeting("Rohit")
g1.full_message()
Expected output:
Hello Rohit, have a great day!
How it works:
g1.full_message()runs withselfset tog1.- Inside it,
self.say_hello()callssay_hello()on the same object. say_hello()returns"Hello Rohit", andfull_message()adds the rest of the message.
Writing say_hello() without self. would raise a NameError, because methods are not standalone functions — they must be accessed through an object.
Parameters vs Attributes
Inside __init__(), it is common to see the same name twice:
def __init__(self, name):
self.name = name
| Name | What It Is | Lifetime |
|---|---|---|
name | The parameter — a local variable holding the argument | Disappears when __init__() ends |
self.name | The attribute — data stored on the object | Lasts as long as the object |
Using the same name is a convention that makes the connection clear; they could have different names.
Common Mistakes
| Mistake | Problem |
|---|---|
Forgetting self in the method definition | TypeError: takes 0 positional arguments but 1 was given |
Writing name = name instead of self.name = name | The attribute is never created |
Calling another method without self. | NameError |
Passing self manually: p1.show_profile(p1) | TypeError — Python passes it automatically |
Renaming self | Works, but confuses readers |
Related Concepts
- Python
__init__()Method — whereselfis first used to set attributes - Python Class Properties — instance and class attributes
- Python Class Methods — methods that use
self, and those that do not