2.1.5 · HinglishOOP Fundamentals

`self` — what it is and how Python passes it

1,578 words7 min readRead in English

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.balance

Python 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
Figure — `self` — what it is and how Python passes it

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 manually

Yeh 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?
Ek reference us specific instance ka jis par method call hua, pehle argument ke roop mein pass hota hai.
alice.deposit(50) kis explicit call ke equivalent hai?
Account.deposit(alice, 50).
Kya self ek Python keyword hai?
Nahi, yeh ek convention hai; koi bhi naam kaam karta hai lekin self standard hai.
def greet(): mein self bhoolne par "takes 0 positional arguments but 1 was given" error kyun aata hai?
Python instance automatically pass karta hai, toh 1 arg ek 0-arg function par pahunchta hai.
Ek bound method ka __self__ kya hota hai?
Woh instance jis se method bound hai.
Method ke andar balance ki jagah self.balance kyun use karna chahiye?
Bare balance ek local variable hai; self.balance instance attribute access karta hai.
self return karna kya enable karta hai?
Method chaining / fluent interfaces (jaise obj.a().b()).
alice.deposit ko alice ke baare mein kaise pata hai?
Descriptor protocol ke through, jo ek bound method return karta hai jisme instance store hota hai.

Connections

Concept Map

shared by many

method needs which one

is a

references

sugar for

binds

triggers

returns

packages

injects as first arg

carries data of

holds

Class blueprint

Instances alice bob

self

Name convention not keyword

Specific instance

instance.method args

Class.method instance, args

Descriptor protocol

Bound method

function + instance

balance owner