2.1.2 · HinglishOOP Fundamentals

`__init__` constructor — initializing attributes

1,638 words7 min readRead in English

2.1.2 · Coding › OOP Fundamentals


WHY __init__ exist karta hai?


WHAT exactly hai __init__?


HOW actually ek call kaam karta hai? (Scratch se derivation)

Chalte hain derive karte hain ki obj = Person("Asha", 30) step by step actually kya karta hai. Kuch bhi magic nahi hai.

Step 1 — Class ko call karna type.__call__ trigger karta hai. Kyun? Ek class khud ek object hai (type ka instance). Usmein () use karna type.__call__ invoke karta hai.

Step 2 — __call__ ek blank object allocate karne ke liye __new__ call karta hai.

obj = Person.__new__(Person)     # empty Person, no attributes yet

Kyun? Fill karne se pehle tumhe ek memory shell chahiye.

Step 3 — __call__ us blank object par __init__ call karta hai.

Person.__init__(obj, "Asha", 30) # self=obj, name="Asha", age=30

Yeh step kyun? Yahan self.name = name actually run hota hai, data attach karta hai.

Step 4 — __call__ obj return karta hai. Kyun? Taaki obj = Person(...) ko finished object mile.

Toh equivalence jo tum derive aur verify kar sakte ho:

Figure — `__init__` constructor — initializing attributes

Worked Examples


Common Mistakes (Steel-manned)


Forecast-then-Verify


Flashcards

__init__ kab call hota hai?
Automatically, naya object create hone ke turant baad (Cls(...) ke dauran).
Kya __init__ object create karta hai?
Nahi — __new__ blank object create karta hai; __init__ sirf uske attributes initialize karta hai.
self kya hai?
Us specific instance ka reference jo initialize ho raha hai; automatically pehle parameter ke roop mein pass hota hai.
__init__ ko kya return karna chahiye?
None (kuch aur return karne par TypeError aata hai).
__init__ mein self.name = name aur name = name mein kya fark hai?
Pehla object par data store karta hai (persist karta hai); doosra sirf ek local variable reassign karta hai (jo __init__ khatam hone par chala jaata hai).
def __init__(self, x=[]) kyun dangerous hai?
Default list ek baar create hoti hai aur sab instances mein share hoti hai; x=None use karo phir andar [] assign karo.
Cls(args) kismein expand hota hai?
o = Cls.__new__(Cls); Cls.__init__(o, args); return o.
__init__ ke andar validation kyun rakhein?
Yeh woh single gateway hai jisse sab objects guzarte hain, isliye yeh guarantee karta hai ki koi invalid object exist nahi karta.

Recall Feynman: ek 12-saal ke bacche ko explain karo

Socho tum ek brand-new school bag khareed rahe ho. Dukaan pehle tumhe ek khali bag deta hai (yeh __new__ hai). Phir, tumhe dene se pehle, woh usmein tumhara name tag, books, aur pencils fill karta hai taaki woh use ke liye ready ho (yeh __init__ hai). self bas "yeh bag" hai — jab woh likhte hain "is bag ka naam = Asha," unka matlab hai tumhara specific bag, kisi aur ka nahi. Har bag usi filling station se guzarta hai, isliye koi bag kabhi khali ya toota hua nahi jaata.


Connections

  • OOP Fundamentals__init__ object creation ki foundation hai.
  • self and instance attributesself.x = ... actually kya store karta hai.
  • __new__ vs __init__ — allocation vs initialization.
  • Class vs Instance Attributes__init__ instance attributes set karta hai.
  • Properties and computed attributes__init__ mein compute karne ka alternative.
  • Mutable default argument pitfall — shared-default trap.
  • Dunder methods__init__ kai special methods mein se ek hai.

Concept Map

invokes

first calls

then calls

finally returns

produces

passed as self

receives

assigns

must return

guarantees

rarely written

usually written

Cls args call

type dot __call__

__new__ allocates blank object

__init__ constructor

finished object

empty shell no attributes

self this object

instance attributes

None else TypeError

every object born valid

you write __init__