self ParameterIntroduction π
The self parameter is one of the core components of Pythonβs Object-Oriented Programming. It represents the **current object** (instance) and allows access to its attributes and methods. Without self, an object wouldn't know its own data!
Note
self is NOT a keyword β itβs just a naming convention. You can rename it, but you should never do that.1. Why Do We Need self? π€
Every object has its own data. The self parameter allows instance methods to refer to **that specific object's attributes**.
why_self.py
class Person:
def __init__(self, name):
self.name = name # self refers to the current object
p1 = Person("Sathish")
p2 = Person("Kumar")
print(p1.name) # Sathish
print(p2.name) # Kumarβ Each object keeps its own values because of self.
2. How Python Passes self Automatically π§
auto_pass_self.py
class Demo:
def show(self):
print("Method called")
d = Demo()
d.show() # Python calls Demo.show(d) internallyβ You NEVER pass self manually.
β Python injects it automatically when calling methods.
3. Using self to Access Instance Attributes π
access_attributes.py
class Car:
def __init__(self, model, year):
self.model = model
self.year = year
def info(self):
print(f"Model: {self.model}, Year: {self.year}")
c = Car("BMW", 2023)
c.info()β self.model & self.year belong to that specific car object.
4. Using self to Modify Instance Attributes π οΈ
modify_attributes.py
class Bank:
def __init__(self, balance):
self.balance = balance
def deposit(self, amount):
self.balance += amount
b = Bank(500)
b.deposit(200)
print(b.balance)β Methods can update object state using self.
5. Calling Other Methods Using self π
call_other_method.py
class Player:
def intro(self):
return "I am a player"
def greet(self):
print("Hello!", self.intro())
p = Player()
p.greet()β self lets methods interact with each other inside the same object.
6. Why self Must Be the First Parameter β οΈ
Python needs consistency. The first parameter of any instance method must ALWAYS refer to the object calling that method.
why_first_param.py
class Demo:
def show(self):
print(self)
d = Demo()
d.show() # prints the memory address of object 'd'7. self Inside __init__ (Constructor) π
self_in_init.py
class User:
def __init__(self, username, password):
self.username = username
self.password = password
u = User("admin", "1234")
print(u.username)β Constructor uses self to assign initial attributes.
8. You *Can* Rename self, BUT You Shouldnβt π
rename_self.py
class Test:
def show(obj):
print("Hello")
t = Test()
t.show()Note
β But VERY bad practice β never rename
self.9. Instance Attribute Storage Using self.__dict__ π¦
dict_attributes.py
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p = Person("Sathish", 23)
print(p.__dict__)β All instance attributes stored in a dictionary internally.
10. Real-World Example: Shopping Cart π
shopping_cart.py
class Cart:
def __init__(self):
self.items = []
def add_item(self, item):
self.items.append(item)
c = Cart()
c.add_item("Laptop")
c.add_item("Mouse")
print(c.items)β self stores each object's unique shopping cart items.
11. Best Practices π‘
- β Always name the first parameter
self - β Use
selfonly for instance-specific data - β Never store unrelated data inside the same object
- β Use
selfto clearly distinguish instance variables from local variables
Conclusion π
self parameter gives identity to objects β it tells each object who it is and what data it owns.β β¨You now fully understand the self Parameter in Python! Want the next topic? Try Constructors, Encapsulation, Magic Methods, or Inheritance. Just tell me! π