2.1.3 · HinglishOOP Fundamentals

Instance attributes vs class attributes

1,607 words7 min readRead in English

2.1.3 · Coding › OOP Fundamentals


DO TARAH ke attributes kyun hote hain?


Yeh precisely KYAN hain

class Dog:
    species = "Canis familiaris"      # class attribute (shared)
 
    def __init__(self, name):
        self.name = name              # instance attribute (per object)

Python actually attributes kaise lookup karta hai (key mechanism)

Yeh ek asymmetry (read = upar search karo, write = hamesha instance pe) neeche ki har tricky behaviour explain karti hai.

Figure — Instance attributes vs class attributes

Lookup rule se "shadowing" behaviour derive karna


Famous trap: mutable class attributes


Common mistakes (Steel-manned)


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

Socho ek classroom hai. Saamne ek whiteboard hai jo sabke saath share hoti hai — agar teacher uspe "Homework: page 5" likhe, toh har student wahi message dekhta hai. Yahi class attribute hai. Har student ke paas apni notebook bhi hai. Main apni notebook mein jo likhta hoon woh sirf mera hai; teri blank rehti hai. Yahi instance attribute hai. Tricky part: agar main "homework change" karna chahoon, toh ya toh (a) apni notebook mein naya note likhoon (ab sirf main apna version dekhta hoon — whiteboard unchanged hai), ya (b) chalkboard tak jaake whiteboard rewrite karoon (sab ka homework change ho jaata hai). Python ka self.x = ... hamesha "apni notebook mein likho" hai; ClassName.x = ... hai "whiteboard rewrite karo".


Active recall

Class attribute physically kahan stored hota hai?
Class ke namespace mein (ClassName.__dict__); saare instances ke liye ek shared copy.
Instance attribute kahan stored hota hai?
Us object ke apne namespace mein (obj.__dict__); har instance ki alag copy.
Python READ pe obj.x kaise resolve karta hai?
Pehle instance __dict__, phir class, phir MRO ke zariye base classes.
obj.x = value hamesha kya karta hai, chahe x class attribute tha ya nahi?
INSTANCE pe x create/update karta hai; class ko kabhi modify nahi karta.
"Shadowing" kya hai?
Jab ek instance attribute same naam ke class attribute ko lookup mein hide karta hai, kyunki instance pehle search hota hai.
Mutable class attribute (jaise tricks = []) instances mein kyun share hota hai?
Kyunki .append() single shared object ka read+mutate hai; bina assignment ke koi per-instance copy kabhi nahi banti.
Shared-mutable-default bug kaise fix karte hain?
__init__ ke andar har instance ke liye fresh object assign karo (self.tricks = []).
Ek class attribute ko SAB instances ke liye ek saath kaise change karte hain?
Class pe hi reassign karo: ClassName.attr = new_value.
a.species = "Wolf" ke baad, Dog.species kya return karega?
Unchanged original class value — a ne sirf ek shadowing instance attribute add kiya.
Kya Python mein class attributes immutable hote hain?
Nahi; "class" sirf batata hai woh kahan rehte hain, immutability nahi — inhe class name ke zariye reassign kiya ja sakta hai.

Connections

  • OOP Fundamentals
  • Classes and objects
  • The self parameter
  • __init__ constructor
  • Inheritance and MRO
  • Namespaces and scope
  • Mutable vs immutable objects

Concept Map

lives on

lives on

one shared copy

one copy per object

read searches

then falls to

then

always targets

never modifies

produces

instance value hides

saves

Class attribute

Class __dict__

Instance attribute

instance __dict__

Shared by all objects

Owned by single object

Lookup rule

Base classes MRO

Write rule

Shadowing

Memory