Instance Variables vs Class (Static) Variables in Python Notes in Hindi & English : इंसटेंस वेरिएबल्स बनाम क्लास (स्टैटिक) वेरिएबल्स में अंतर और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Instance Variables vs Class (Static) Variables in Python (इंसटेंस वेरिएबल्स बनाम क्लास (स्टैटिक) वेरिएबल्स में अंतर) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: Instance variables are bound to individual objects through self and hold state unique to each object, whereas class variables are defined directly inside the class body and shared across all instances. Knowing Python's attribute lookup order prevents shadowing bugs and accidental mutation of shared mutable data.

अध्याय सार: इंसटेंस वेरिएबल्स self के माध्यम से प्रत्येक ऑब्जेक्ट से अलग-अलग जुड़े होते हैं और ऑब्जेक्ट का निजी डेटा रखते हैं, जबकि क्लास (स्टैटिक) वेरिएबल्स सीधे क्लास बॉडी में परिभाषित होकर सभी इंस्टेंस के बीच साझा रहते हैं। एट्रिब्यूट लुकअप क्रम और शैडोइंग नियमों को समझना साझा म्यूटेबल डेटा की त्रुटियों से बचाता है।

Instance Variables vs Class (Static) Variables in Python: Detailed Notes & Architecture (इंसटेंस वेरिएबल्स बनाम क्लास (स्टैटिक) वेरिएबल्स में अंतर)

1. Instance Variables and Per-Object Namespaces (इंसटेंस वेरिएबल्स और प्रत्येक ऑब्जेक्ट का नेमस्पेस)

[EN] Instance variables are typically initialized inside __init__() via self.name = name or attached later on an object reference. Python stores these attributes inside each object's own obj.__dict__ dictionary, ensuring complete isolation between objects of the same class. Modifying e1.name never alters e2.name, and deleting an instance attribute with del e1.name removes it solely from e1.__dict__.

[HI] इंसटेंस वेरिएबल्स सामान्यतः __init__() के अंदर self.name = name के रूप में या बाद में ऑब्जेक्ट रेफरेंस पर सेट किए जाते हैं। पायथन इन्हें प्रत्येक ऑब्जेक्ट की अलग obj.__dict__ डिक्शनरी में रखता है, जिससे एक ही क्लास के दो ऑब्जेक्ट्स का डेटा पूरी तरह स्वतंत्र रहता है। e1.name बदलने या del e1.name करने का असर e2 पर नहीं पड़ता।

2. Class (Static) Variables and Shared State (क्लास (स्टैटिक) वेरिएबल्स और साझा डेटा)

[EN] Class variables are declared inside the class block outside any method, such as company = 'TechCorp', and live in ClassName.__dict__. Because only one copy exists in memory for the class, class variables are ideal for shared constants, configuration defaults, and instance counters. They can be read via either ClassName.var or obj.var (when no matching instance attribute exists), though updates should always target ClassName.var or cls.var inside a @classmethod.

[HI] क्लास वेरिएबल्स किसी भी मेथड से बाहर सीधे क्लास ब्लॉक में (जैसे company = 'TechCorp') घोषित किए जाते हैं और ClassName.__dict__ में संग्रहीत होते हैं। मेमोरी में इनकी केवल एक ही प्रति बनती है, अतः ये साझा कॉन्स्टैंट और ऑब्जेक्ट काउंटर गिनने के लिए उपयुक्त होते हैं। इन्हें ClassName.var या obj.var से पढ़ा जा सकता है, किंतु इन्हें संशोधित करने के लिए हमेशा ClassName.var या @classmethod में cls.var का उपयोग करना चाहिए।

3. Attribute Lookup Order and the Shadowing Trap (एट्रिब्यूट खोज क्रम और शैडोइंग (Shadowing) की समस्या)

[EN] When reading obj.company, Python searches obj.__dict__ first and falls back to type(obj).__dict__ (and parent MRO classes) only if absent on the instance. Consequently, executing e1.company = 'StartupX' does NOT mutate Employee.company; instead, it inserts a brand-new 'company' key into e1.__dict__ that shadows the class variable for e1 alone. Running del e1.company immediately unmasks Employee.company again.

[HI] जब obj.company पढ़ा जाता है, तो पायथन पहले obj.__dict__ देखता है और न मिलने पर ही क्लास के __dict__ तथा MRO में खोजता है। इसलिए e1.company = 'StartupX' लिखने पर Employee.company नहीं बदलता, बल्कि केवल e1.__dict__ में एक नया इंसटेंस एट्रिब्यूट बन जाता है जो e1 के लिए क्लास वेरिएबल को ढक (shadow) लेता है। यदि del e1.company कर दिया जाए, तो e1.company पुनः क्लास वेरिएबल Employee.company को दिखाने लगेगा।

4. The Mutable Class Variable Pitfall (म्यूटेबल क्लास वेरिएबल्स का खतरनाक जाल)

[EN] A notorious Python exam trap arises when a class variable references a mutable object such as skills = []. Executing e1.skills.append('Python') performs an attribute lookup—not an assignment—so Python locates Employee.skills and mutates the shared list in place, making 'Python' visible in e2.skills too. To prevent cross-instance data leakage, keep class variables immutable or initialize mutable collections explicitly per instance inside __init__().

[HI] पायथन परीक्षाओं में एक प्रमुख प्रश्न तब बनता है जब क्लास वेरिएबल कोई म्यूटेबल ऑब्जेक्ट (जैसे skills = []) हो। e1.skills.append('Python') लिखने पर कोई असाइनमेंट (=) न होने के कारण शैडोइंग नहीं होती, बल्कि साझा लिस्ट ही इन-प्लेस बदल जाती है और 'Python' e2.skills में भी दिखने लगता है। इस त्रुटि से बचने के लिए प्रत्येक ऑब्जेक्ट की अलग लिस्ट हमेशा __init__() के भीतर self.skills = [] से बनानी चाहिए।

Syntax, Formula & Technical Example (Instance Variables vs Class (Static) Variables in Python)

class Employee:
    company = 'TechCorp'
    count = 0
    def __init__(self, name):
        self.name = name
        Employee.count += 1

e1, e2 = Employee('Raj'), Employee('Simran')
e1.company = 'StartupX'
print(e1.company, e2.company, Employee.count)

Instance Variables vs Class (Static) Variables in Python Quick Reference & Comparison Table

Comparison AspectInstance VariableClass (Static) Variable
Declaration LocationBound to self inside methodsDefined directly inside class body
Namespace StorageStored inside obj.__dict__Stored inside ClassName.__dict__
Memory SharingSeparate copy per object instanceSingle shared copy across all instances
Assignment Effectself.x = v affects single objectobj.x = v shadows rather than updates class var

Instance Variables vs Class (Static) Variables in Python Top 20 Bilingual MCQs with Answers

Q1. Where are instance variables stored by default for a standard Python object obj?

(एक सामान्य पायथन ऑब्जेक्ट obj के इंसटेंस वेरिएबल्स डिफ़ॉल्ट रूप से कहाँ संग्रहीत होते हैं?)

(A) ClassName.__dict__

(B) sys.__globals__

(C) obj.__slots__

(D) obj.__dict__

Correct Answer: (D)

[EN] Each standard instance stores its own attributes inside its private obj.__dict__ mapping.

[HI] प्रत्येक सामान्य ऑब्जेक्ट अपने इंसटेंस एट्रिब्यूट्स को अपनी निजी obj.__dict__ डिक्शनरी में रखता है।

Q2. What does the Employee snippet above print?

(ऊपर दिए गए Employee कोड स्निपेट का आउटपुट क्या होगा?)

(A) TechCorp TechCorp 2

(B) StartupX TechCorp 1

(C) StartupX TechCorp 2

(D) StartupX StartupX 2

Correct Answer: (C)

[EN] e1.company = 'StartupX' creates a shadowing instance attribute on e1 only; e2 still sees 'TechCorp' and Employee.count is 2.

[HI] e1.company = 'StartupX' केवल e1 पर शैडोइंग इंसटेंस वेरिएबल बनाता है; e2 के लिए TechCorp और Employee.count 2 रहता है।

Q3. If class Dog defines tricks = [] as a class variable and d1.tricks.append('sit') is called, what is d2.tricks?

(यदि class Dog में क्लास वेरिएबल tricks = [] हो और d1.tricks.append('sit') चलाया जाए, तो d2.tricks क्या होगा?)

(A) ['sit', 'sit']

(B) AttributeError

(C) []

(D) ['sit']

Correct Answer: (D)

[EN] append() mutates the shared list object referenced by Dog.tricks in place without creating a shadowing attribute on d1.

[HI] append() बिना नया इंसटेंस वेरिएबल बनाए साझा Dog.tricks लिस्ट को ही बदल देता है, अतः d2.tricks भी ['sit'] दिखेगा।

Q4. When looking up obj.attr in Python, which namespace is searched FIRST?

(पायथन में obj.attr का मान खोजते समय सबसे पहले किस नेमस्पेस को जांचा जाता है?)

(A) Instance namespace (obj.__dict__) after checking data descriptors on the class

(B) Module global namespace

(C) Grandparent class __dict__

(D) Built-in namespace

Correct Answer: (A)

[EN] For normal attributes, Python checks obj.__dict__ before falling back to ClassName.__dict__ and base classes.

[HI] सामान्य एट्रिब्यूट्स के लिए पायथन क्लास और पेरेंट क्लासेस से पहले इंस्टेंस की obj.__dict__ की जांच करता है।

Q5. Suppose e1.company shadowed Employee.company, and then del e1.company is executed. What happens when reading e1.company next?

(मान लें e1.company ने Employee.company को शैडो किया था और फिर del e1.company चलाया गया। अब e1.company पढ़ने पर क्या होगा?)

(A) It deletes Employee.company for all objects

(B) It returns None

(C) It raises AttributeError because company was deleted from the class

(D) It falls back and returns Employee.company ('TechCorp')

Correct Answer: (D)

[EN] del e1.company deletes only the instance entry in e1.__dict__, unmasking the class variable again.

[HI] del e1.company केवल e1.__dict__ से एंट्री हटाता है, जिससे क्लास वेरिएबल पुनः दिखाई देने लगता है।

Q6. In __init__, why should an instance counter write Employee.count += 1 instead of self.count += 1?

(__init__ में ऑब्जेक्ट्स की संख्या गिनने के लिए self.count += 1 के बजाय Employee.count += 1 क्यों लिखना चाहिए?)

(A) self.count += 1 creates a new instance variable on each object stuck at 1

(B) self.count raises a SyntaxError inside __init__

(C) Employee.count runs faster using C pointers

(D) self.count decrements the counter instead

Correct Answer: (A)

[EN] self.count += 1 reads Class.count (0), adds 1, and assigns the result back to self.__dict__, never updating Employee.count.

[HI] self.count += 1 क्लास से मान पढ़कर उसे self.__dict__ में सहेजता है, जिससे Employee.count कभी 0 से आगे नहीं बढ़ता।

Q7. Which decorator should a method use when its purpose is to read or modify a class variable across subclasses cleanly?

(किसी क्लास वेरिएबल को सबक्लासेस के अनुसार भी सही ढंग से पढ़ने या बदलने के लिए मेथड पर कौन-सा डेकोरेटर लगाना चाहिए?)

(A) @property

(B) @instancemethod

(C) @classmethod

(D) @staticmethod

Correct Answer: (C)

[EN] @classmethod receives cls, allowing cls.var to target the specific class or subclass calling the method.

[HI] @classmethod को cls प्राप्त होता है, जिससे cls.var संबंधित क्लास या सबक्लास के वेरिएबल पर कार्य कर सकता है।

Q8. Where does Python store class variables defined directly in the class body?

(क्लास बॉडी में सीधे परिभाषित किए गए क्लास वेरिएबल्स को पायथन कहाँ सहेजता है?)

(A) Each instance's __dict__

(B) function.__code__

(C) builtins.__dict__

(D) ClassName.__dict__

Correct Answer: (D)

[EN] Class-level variables and method definitions reside in ClassName.__dict__ (a mappingproxy).

[HI] क्लास स्तर के वेरिएबल्स और मेथड परिभाषाएं ClassName.__dict__ में संग्रहीत होती हैं।

Q9. What happens if a subclass defines a class variable with the same name as a parent class variable?

(यदि कोई सबक्लास अपने पेरेंट क्लास के समान नाम का एक नया क्लास वेरिएबल परिभाषित करे तो क्या होगा?)

(A) TypeError is raised for duplicate static names

(B) The parent's class variable is permanently overwritten

(C) Both values are merged into a tuple

(D) The subclass variable shadows the parent variable for subclass objects without altering the parent's value

Correct Answer: (D)

[EN] Each class keeps its own __dict__, so Child.rate overrides Parent.rate only along lookups starting at Child.

[HI] प्रत्येक क्लास अपनी अलग __dict__ रखती है, अतः Child.rate बिना Parent.rate बदले सबक्लास के लिए मान बदल देता है।

Q10. Which of the following best describes a class variable in Python?

(पायथन में क्लास वेरिएबल का सबसे सटीक वर्णन निम्नलिखित में से कौन-सा है?)

(A) A constant created strictly with final keyword

(B) A local variable inside an instance method

(C) A variable declared inside __init__ using global keyword

(D) A variable defined inside a class outside any method and shared across all instances

Correct Answer: (D)

[EN] Python has no 'static' or 'final' keywords; class-body variables serve as shared class (static) variables.

[HI] पायथन में static कीवर्ड नहीं होता; किसी मेथड के बाहर सीधे क्लास बॉडी में बने वेरिएबल ही साझा क्लास वेरिएबल होते हैं।

Q11. In Python, what happens if you read Employee.name when 'name' is only an instance variable initialized inside __init__?

(यदि 'name' केवल __init__ में बना एक इंसटेंस वेरिएबल हो और आप Employee.name पढ़ने का प्रयास करें, तो क्या होगा?)

(A) It returns an empty string ''

(B) AttributeError: type object 'Employee' has no attribute 'name'

(C) It returns the name of the first Employee created

(D) It returns None

Correct Answer: (B)

[EN] Instance attributes exist inside individual obj.__dict__ dictionaries and are not accessible on the class object.

[HI] इंसटेंस वेरिएबल्स केवल ऑब्जेक्ट की __dict__ में होते हैं, अतः क्लास के नाम से उन्हें पढ़ने पर AttributeError आता है।

Q12. What is the return type of ClassName.__dict__ in Python 3?

(पायथन 3 में ClassName.__dict__ का डेटा टाइप क्या होता है?)

(A) mutable list

(B) tuple

(C) set

(D) mappingproxy (read-only view of the class namespace)

Correct Answer: (D)

[EN] Python protects class namespaces with a read-only mappingproxy, forcing modifications through setattr or dot syntax.

[HI] क्लास नेमस्पेस की सुरक्षा के लिए पायथन ClassName.__dict__ को रीड-ओनली mappingproxy के रूप में रखता है।

Q13. If class Box defines tag = 'A', and b = Box(); Box.tag = 'B', what does b.tag evaluate to?

(यदि class Box में tag = 'A' हो तथा b = Box(); Box.tag = 'B' चलाया जाए, तो b.tag का मान क्या होगा?)

(A) 'A'

(B) None

(C) AttributeError

(D) 'B'

Correct Answer: (D)

[EN] Because b has no 'tag' in b.__dict__, b.tag looks up Box.tag dynamically and sees the updated 'B'.

[HI] b.__dict__ में 'tag' न होने के कारण b.tag सीधे Box.tag को देखता है और नया मान 'B' लौटाता है।

Q14. Why does Python allow accessing class variables via self.class_var inside methods?

(पायथन मेथड्स के अंदर self.class_var लिखकर क्लास वेरिएबल पढ़ने की अनुमति क्यों देता है?)

(A) Attribute lookup checks the instance first and automatically falls back to the class namespace

(B) Class variables are copied into every instance automatically at creation

(C) self is an alias for ClassName

(D) Because self.class_var bypasses __dict__ entirely

Correct Answer: (A)

[EN] Reading self.class_var searches self.__dict__, fails to find it, and continues up the class and MRO chain.

[HI] self.class_var पढ़ने पर पहले इंस्टेंस खोजा जाता है और न मिलने पर स्वतः क्लास नेमस्पेस से मान लाया जाता है।

Q15. Which statement about instance vs class variables in Python is NOT true?

(पायथन में इंसटेंस बनाम क्लास वेरिएबल्स के बारे में निम्नलिखित में से कौन-सा कथन असत्य है?)

(A) Class variables exist even before any instance of the class is created

(B) Assigning obj.class_var = 10 updates the class variable for every instance of that class

(C) Mutable class variables can unintentionally share state across instances

(D) Instance variables consume memory separately for each created object

Correct Answer: (B)

[EN] Assigning via an instance reference creates a shadowing instance variable on obj instead of updating the class variable.

[HI] ऑब्जेक्ट रेफरेंस के जरिए असाइन करने से क्लास वेरिएबल नहीं बदलता, बल्कि केवल उस ऑब्जेक्ट में शैडोइंग वेरिएबल बनता है।

Q16. How many times is a class variable allocated in memory if 1,000 instances of that class are created?

(यदि किसी क्लास के 1,000 ऑब्जेक्ट बनाए जाएं, तो उस क्लास के क्लास वेरिएबल के लिए मेमोरी में कितनी प्रतियां बनेंगी?)

(A) 1,000 separate copies

(B) 1 single shared copy on the class

(C) 1,001 copies

(D) 0 copies until read

Correct Answer: (B)

[EN] Class variables belong to the class object itself and exist as one shared reference regardless of instance count.

[HI] क्लास वेरिएबल्स स्वयं क्लास से जुड़े होते हैं, अतः ऑब्जेक्ट्स की संख्या चाहे जितनी हो, मेमोरी में केवल 1 साझा प्रति रहती है।

Q17. Within Instance Variables vs Class (Static) Variables in Python, which comparison aspect directly maps to: "Bound to self inside methods"?

(Instance Variables vs Class (Static) Variables in Python में कौन सा Comparison Aspect सीधे "Bound to self inside methods" से संबंधित है?)

(A) Assignment Effect

(B) Declaration Location

(C) Namespace Storage

(D) Memory Sharing

Correct Answer: (B)

[EN] In Instance Variables vs Class (Static) Variables in Python, 'Declaration Location' handles 'Bound to self inside methods' (Defined directly inside class body).

[HI] Instance Variables vs Class (Static) Variables in Python में 'Declaration Location' का कार्य/विवरण 'Bound to self inside methods' (Defined directly inside class body) है।

Q18. Which option accurately describes 'Namespace Storage' in Instance Variables vs Class (Static) Variables in Python?

(Instance Variables vs Class (Static) Variables in Python के संदर्भ में 'Namespace Storage' की सही विशेषता कौन सी है?)

(A) self.x = v affects single object

(B) Stored inside obj.__dict__

(C) Separate copy per object instance

(D) Bound to self inside methods

Correct Answer: (B)

[EN] 'Namespace Storage' provides 'Stored inside obj.__dict__' — key point: Stored inside ClassName.__dict__.

[HI] 'Namespace Storage' का मुख्य विवरण 'Stored inside obj.__dict__' है (Stored inside ClassName.__dict__)।

Q19. In Instance Variables vs Class (Static) Variables in Python, identify the correct parameter for the specification: "Separate copy per object instance".

(Instance Variables vs Class (Static) Variables in Python में "Separate copy per object instance" विवरण किस विकल्प के लिए सही है?)

(A) Namespace Storage

(B) Declaration Location

(C) Assignment Effect

(D) Memory Sharing

Correct Answer: (D)

[EN] 'Memory Sharing' corresponds to 'Separate copy per object instance' (Single shared copy across all instances).

[HI] 'Memory Sharing' का संबंध 'Separate copy per object instance' (Single shared copy across all instances) से है।

Q20. Considering Instance Variables vs Class (Static) Variables in Python, what key behavior defines 'Assignment Effect'?

(Instance Variables vs Class (Static) Variables in Python में 'Assignment Effect' की मुख्य भूमिका या मान क्या है?)

(A) Separate copy per object instance

(B) Bound to self inside methods

(C) self.x = v affects single object

(D) Stored inside obj.__dict__

Correct Answer: (C)

[EN] 'Assignment Effect' represents 'self.x = v affects single object' (obj.x = v shadows rather than updates class var).

[HI] 'Assignment Effect' का अर्थ/मान 'self.x = v affects single object' है (obj.x = v shadows rather than updates class var)।

Instance Variables vs Class (Static) Variables in Python — Exam FAQs

Q1: Why did changing a variable through one object affect other objects in my Python class?

मेरी पायथन क्लास में एक ऑब्जेक्ट से वेरिएबल बदलने पर बाकी ऑब्जेक्ट्स का डेटा क्यों बदल गया?

[EN] You likely declared a mutable object (such as a list or dict) at the class level and modified it in place using .append() or key assignment instead of initializing self.items = [] inside __init__().

[HI] संभवतः आपने क्लास स्तर पर कोई म्यूटेबल ऑब्जेक्ट (जैसे list या dict) बनाया और __init__ में अलग कॉपी बनाने के बजाय सीधे .append() से उसी साझा ऑब्जेक्ट को बदल दिया।

Q2: How do I create a static variable in Python like in Java or C++?

जावा या C++ की तरह पायथन में स्टैटिक वेरिएबल कैसे बनाया जाता है?

[EN] Python has no 'static' keyword for variables; instead, any variable assigned directly inside the class body outside all methods acts as a class (static) variable shared by all instances.

[HI] पायथन में वेरिएबल के लिए static कीवर्ड नहीं होता; मेथड्स से बाहर सीधे क्लास बॉडी में परिभाषित किया गया कोई भी वेरिएबल स्टैटिक (क्लास) वेरिएबल की तरह कार्य करता है।

Q3: What happens if an instance variable and a class variable have the exact same name?

यदि किसी इंसटेंस वेरिएबल और क्लास वेरिएबल का नाम बिल्कुल एक जैसा हो तो क्या होगा?

[EN] When accessed through the instance (obj.x), the instance variable shadows and hides the class variable because Python inspects obj.__dict__ before checking ClassName.__dict__.

[HI] जब ऑब्जेक्ट (obj.x) से एक्सेस किया जाता है, तो इंसटेंस वेरिएबल क्लास वेरिएबल को ढक (shadow) लेता है क्योंकि पायथन क्लास से पहले obj.__dict__ को देखता है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)