`self` — what it is and how Python passes it
2.1.5· Coding › OOP Fundamentals
self KYA hai?
YEH EXIST KYUN KARTA HAI? Ek class ek blueprint hai jo kai objects share karte hain. Ek def deposit(...) body ko alice ke account aur bob ke account dono ke liye kaam karna padta hai. Method ko jaanna hota hai ki kaunse account ka balance change karna hai. self wahi "kaun sa wala" hai — yeh current object ka data carry karta hai.
class Account:
def __init__(self, owner, balance):
self.owner = owner # store ON this instance
self.balance = balance
def deposit(self, amount):
self.balance += amount # change THIS instance's balance
return self.balancePython self kaise pass karta hai (core mechanism)
Yeh dono lines equivalent hain:
Toh self ← alice aur amount ← 50. Bas yahi poora trick hai. Koi hidden global nahi, koi magic nahi — sirf argument binding.
alice.deposit ko alice ke baare mein kaise pata hai?
Descriptor protocol ki wajah se. Ek class mein stored function ek descriptor hota hai: jab aap ise ek instance ke through access karte hain (alice.deposit), Python ek bound method return karta hai — ek chhota object jo instance aur function dono yaad rakhta hai:
print(alice.deposit) # <bound method Account.deposit of <Account...>>
print(alice.deposit.__self__) # the alice instance itself
print(alice.deposit.__func__) # the underlying plain function
Scratch se derivation: self ko bina classes ke rebuild karo
Yeh prove karne ke liye ki self sirf ek argument hai, chaliye plain functions aur dicts use karke methods simulate karte hain (ek object hai "data + functions jo woh data lete hain").
# An "object" is just a dict of data.
def make_account(owner, balance):
return {"owner": owner, "balance": balance}
# A "method" is a plain function whose first param is the object.
def deposit(self, amount): # <-- self chosen by us, nothing special
self["balance"] += amount
return self["balance"]
alice = make_account("Alice", 100)
print(deposit(alice, 50)) # 150 -- WE pass the object manuallyYeh step kyun? Yeh dikhata hai ki real classes mein "automatic" self exactly yahi manual passing hai — Python bas aapke liye deposit(alice, ...) waala hissa dot ke through kar deta hai.
Worked examples
Common mistakes (steel-manned)
Active recall
Recall Q:
obj.m(x) andar se kya translate hota hai?
Class.m(obj, x) — instance pehla positional argument ban jaata hai.
Recall Q: Kya
self ek keyword hai?
Nahi — yeh ek naming convention hai. Koi bhi valid naam kaam karta hai, lekin hamesha self use karo.
Recall Q:
self.balance kyun likhna padta hai, sirf balance kyun nahi?
Bare balance ek local variable hai. Instance attributes object par rehte hain, self. ke through access hote hain.
Recall Q: Bound method kya hota hai?
function + instance ka ek package; ise call karne par woh instance automatically pehla arg inject ho jaata hai.
Recall Feynman:
self ko ek 12 saal ke bachche ko explain karo
Socho ek recipe (the class) ek kitchen mein lagi hui hai. Kai cooks (objects) ek hi recipe use karte hain. Jab ek cook yeh step follow karta hai "apne bowl mein cheeni daalo", toh self hai "apna bowl" — yeh recipe ko batata hai ki kis cook ka bowl use karna hai. Recipe nahi badalti; sirf kiska bowl badalta hai. Jab tum ek cook ko kehte ho "cake banao!", tum automatically unhe unka apna bowl de dete ho — yahi dena Python ka self pass karna hai.
Flashcards
Python method mein self kya hai?
alice.deposit(50) kis explicit call ke equivalent hai?
Account.deposit(alice, 50).Kya self ek Python keyword hai?
self standard hai.def greet(): mein self bhoolne par "takes 0 positional arguments but 1 was given" error kyun aata hai?
Ek bound method ka __self__ kya hota hai?
Method ke andar balance ki jagah self.balance kyun use karna chahiye?
balance ek local variable hai; self.balance instance attribute access karta hai.self return karna kya enable karta hai?
obj.a().b()).alice.deposit ko alice ke baare mein kaise pata hai?
Connections
- OOP Fundamentals
- Classes and Instances
- __init__ — the constructor
- Instance vs Class Attributes
- Bound and Unbound Methods
- Descriptor Protocol
- classmethod and staticmethod (kaise
clsaur koi first-arg nahi,selfse alag hain)