PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Inheritance in Python: Single, Multiple, Multilevel, super() Function (इनहेरिटेंस: सिंगल, मल्टीपल, सुपर() फंक्शन और MRO) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Inheritance allows a subclass to acquire attributes and methods from one or more base classes, promoting code reuse and modular hierarchies. Python supports single, multilevel, hierarchical, and multiple inheritance, resolving lookups cleanly via C3 Linearization Method Resolution Order (MRO) and super().
अध्याय सार: इनहेरिटेंस के माध्यम से सबक्लास एक या अधिक बेस क्लासेस के एट्रिब्यूट्स और मेथड्स प्राप्त करती है, जिससे कोड का पुनः उपयोग आसान होता है। पायथन सिंगल, मल्टीलेवल, हायराक्रिकल और मल्टीपल इनहेरिटेंस को सपोर्ट करता है तथा डायमंड समस्या से बचने के लिए C3 लीनियराइजेशन (MRO) और super() फंक्शन का उपयोग करता है।
Inheritance in Python: Single, Multiple, Multilevel, super() Function: Detailed Notes & Architecture (इनहेरिटेंस: सिंगल, मल्टीपल, सुपर() फंक्शन और MRO)
1. Forms of Inheritance and Class Introspection (इनहेरिटेंस के प्रकार और क्लास जांच टूल्स)
[EN] Python supports single inheritance class Dog(Animal):, multilevel chains class Puppy(Dog):, hierarchical branching where multiple subclasses share one parent, and multiple inheritance class D(B, C): where a child inherits from several bases simultaneously. Built-in introspection relies on isinstance(obj, Cls) to test whether obj belongs to Cls or any subclass, and issubclass(Sub, Base) to verify class relationships (where every class is considered a subclass of itself and of object).
[HI] पायथन सिंगल (class Dog(Animal):), मल्टीलेवल (class Puppy(Dog):), हायराक्रिकल (एक पेरेंट से कई चाइल्ड) और मल्टीपल इनहेरिटेंस (class D(B, C):) सभी को सपोर्ट करता है। इनकी जांच के लिए isinstance(obj, Cls) ऑब्जेक्ट के प्रकार की पुष्टि करता है तथा issubclass(Sub, Base) यह बताता है कि Sub, Base का सबक्लास है या नहीं (प्रत्येक क्लास स्वयं की और object की सबक्लास मानी जाती है)।
2. Method Resolution Order (MRO) and C3 Linearization (मेथड रेजोल्यूशन ऑर्डर (MRO) और C3 लीनियराइजेशन)
[EN] In multiple and diamond inheritance (such as class D(B, C) where both B and C inherit from A), ambiguity could arise over which ancestor method runs first. Since Python 2.3 and exclusively in Python 3, Python computes a deterministic Method Resolution Order using the C3 Linearization algorithm. C3 preserves local left-to-right base order (B before C) and monotonicity, ensuring shared ancestors like A appear only once at the end; invalid hierarchies raise a TypeError at class definition time.
[HI] मल्टीपल और डायमंड इनहेरिटेंस (जैसे class D(B, C) जहाँ B और C दोनों A से निकले हों) में मेथड खोज के असमंजस को सुलझाने के लिए पायथन 3 C3 लीनियराइजेशन एल्गोरिदम से MRO तय करता है। C3 बाएँ से दाएँ प्राथमिकता (B पहले, फिर C) और मोनोटोनिसिटी बनाए रखता है, जिससे साझा पूर्वज A केवल अंत में एक बार आता है। असंगत पदानुक्रम बनाने पर क्लास परिभाषा के समय ही TypeError आ जाता है।
3. Cooperative Delegation with the super() Function (super() फंक्शन द्वारा कोऑपरेटिव इनहेरिटेंस)
[EN] Contrary to common belief, super() does not strictly call the immediate parent class; rather, it returns a proxy object that delegates lookup to the NEXT class in the current object's MRO list. In Python 3, zero-argument super() works automatically inside any method, replacing Python 2's super(Child, self). When every class in a diamond hierarchy calls super(), the shared base class A runs exactly once instead of being invoked redundantly by both B and C.
[HI] आमतौर पर माना जाता है कि super() केवल सीधी पेरेंट क्लास को कॉल करता है, जबकि वास्तव में यह वर्तमान ऑब्जेक्ट के MRO क्रम में अगली क्लास को मेथड कॉल सौंपता है। पायथन 3 में बिना आर्ग्यूमेंट के super() स्वतः कार्य करता है (पायथन 2 में super(Child, self) लिखना पड़ता था)। डायमंड पदानुक्रम में सभी क्लासेस द्वारा super() उपयोग करने पर साझा बेस क्लास A केवल एक बार निष्पादित होती है।
4. Root object Class, Built-in Subclassing, and Mixins (रूट object क्लास, बिल्ट-इन सबक्लासिंग और मिक्सिन्स)
[EN] Every Python 3 class defined without explicit bases implicitly inherits from the root object class, gaining default implementations of __repr__, __eq__, and __hash__. Python allows developers to subclass built-in types like list, dict, or Exception directly to add specialized domain behavior. Additionally, Mixin classes are small single-purpose classes placed on the left in multiple inheritance (e.g., class LogUser(LogMixin, User):) to compose reusable capabilities without deep hierarchies.
[HI] पायथन 3 में बिना किसी पेरेंट के बनाई गई प्रत्येक क्लास स्वतः रूट object क्लास से इनहेरिट होती है और __repr__, __eq__ तथा __hash__ जैसे मेथड प्राप्त करती है। पायथन में list, dict या Exception जैसे बिल्ट-इन टाइप्स को भी सीधे इनहेरिट किया जा सकता है। बड़े प्रोजेक्ट में गहरे पदानुक्रम से बचने के लिए Mixin क्लासेस को मल्टीपल इनहेरिटेंस में बाईं ओर रखकर अतिरिक्त फीचर्स जोड़े जाते हैं।
Syntax, Formula & Technical Example (Inheritance in Python: Single, Multiple, Multilevel, super() Function)
class A:
def show(self): return ['A']
class B(A):
def show(self): return ['B'] + super().show()
class C(A):
def show(self): return ['C'] + super().show()
class D(B, C):
pass
print(D().show(), [c.__name__ for c in D.__mro__[:4]])
Inheritance in Python: Single, Multiple, Multilevel, super() Function Quick Reference & Comparison Table
| Inheritance Feature | Syntax / Mechanism | Exam Key Point |
|---|---|---|
| Single Inheritance | class Child(Parent): | One child inherits one parent class |
| Multiple Inheritance | class D(B, C): | Left-to-right base order guides MRO |
| Multilevel Inheritance | class C(B): with class B(A): | Sequential chain across generations |
| C3 MRO & super() | Cls.__mro__ / Cls.mro() | Delegates to next MRO class, not blind parent |
Inheritance in Python: Single, Multiple, Multilevel, super() Function Top 20 Bilingual MCQs with Answers
Q1. Which algorithm does Python 3 use to compute the Method Resolution Order (MRO) for classes?
(पायथन 3 क्लासेस के लिए मेथड रेजोल्यूशन ऑर्डर (MRO) की गणना करने हेतु किस एल्गोरिदम का उपयोग करता है?)
(A) Depth-First Left-to-Right (DLR)
(B) C3 Linearization
(C) Round-Robin Priority Queue
(D) Breadth-First Search only
Correct Answer: (B)
[EN] Python 3 computes all new-style class MROs deterministically using C3 Linearization.
[HI] पायथन 3 सभी क्लासेस के लिए MRO की गणना C3 लीनियराइजेशन एल्गोरिदम से करता है।
Q2. What does D().show() return in the diamond inheritance snippet above?
(ऊपर दिए गए डायमंड इनहेरिटेंस कोड में D().show() क्या लौटाएगा?)
(A) ['B', 'C', 'A']
(B) ['B', 'A', 'C', 'A']
(C) ['B', 'A']
(D) ['C', 'B', 'A']
Correct Answer: (A)
[EN] D's MRO is [D, B, C, A, object]. B calls super().show() -> C.show(), which calls super().show() -> A.show() once.
[HI] D का MRO [D, B, C, A, object] है। B का super() आगे C को और C का super() एक बार A को कॉल करता है, अतः ['B', 'C', 'A'] मिलता है।
Q3. What is the exact return type difference between Cls.__mro__ and Cls.mro()?
(Cls.__mro__ और Cls.mro() के रिटर्न टाइप में क्या अंतर होता है?)
(A) mro() works only in Python 2
(B) __mro__ excludes object whereas mro() includes it
(C) __mro__ returns strings whereas mro() returns integers
(D) __mro__ is a tuple of classes whereas mro() returns a list of classes
Correct Answer: (D)
[EN] Cls.__mro__ stores a read-only tuple of classes, while the Cls.mro() method returns a list.
[HI] Cls.__mro__ क्लासेस का एक टपल होता है जबकि Cls.mro() मेथड क्लासेस की एक लिस्ट लौटाता है।
Q4. In Python 3, which class does class Sample: pass implicitly inherit from?
(पायथन 3 में class Sample: pass लिखने पर वह क्लास अप्रत्यक्ष रूप से किससे इनहेरिट होती है?)
(A) object
(B) None (it has no base class)
(C) type
(D) BaseClass
Correct Answer: (A)
[EN] Every class in Python 3 implicitly inherits from the root object class.
[HI] पायथन 3 में बिना किसी पेरेंट के लिखी गई प्रत्येक क्लास स्वतः रूट object क्लास से इनहेरिट होती है।
Q5. What does issubclass(Student, Student) evaluate to in Python?
(पायथन में issubclass(Student, Student) का परिणाम क्या होता है?)
(A) TypeError
(B) True
(C) False
(D) None
Correct Answer: (B)
[EN] In Python, every class is considered a subclass of itself, so issubclass(C, C) always returns True.
[HI] पायथन में प्रत्येक क्लास स्वयं की सबक्लास मानी जाती है, इसलिए issubclass(C, C) हमेशा True देता है।
Q6. Which type of inheritance is formed when classes B and C inherit from A, and class D inherits from both B and C?
(जब B और C दोनों A से इनहेरिट हों और D इन दोनों (B, C) से इनहेरिट हो, तो यह किस समस्या/संरचना को दर्शाता है?)
(A) Diamond (Hybrid) Inheritance
(B) Single Linear Inheritance
(C) Circular Inheritance
(D) Interface-only Inheritance
Correct Answer: (A)
[EN] Combining hierarchical branching (B, C from A) with multiple inheritance (D from B, C) creates the diamond hierarchy.
[HI] एक बेस से दो शाखाएं और फिर दोनों से एक चाइल्ड बनना डायमंड (हाइब्रिड) इनहेरिटेंस कहलाता है।
Q7. In Python 3, what does super() inside a method actually delegate to?
(पायथन 3 में किसी मेथड के भीतर super() वास्तव में किसे कॉल करता है?)
(A) Always the root object class directly
(B) Strictly the first parent listed inside parentheses only
(C) The child class calling the method
(D) The next class after the current class in the instance's MRO
Correct Answer: (D)
[EN] super() inspects the dynamic MRO of type(self) and forwards the lookup to the next class in sequence.
[HI] super() ऑब्जेक्ट के वर्तमान MRO क्रम में अगली क्लास को मेथड लुकअप सौंपता है।
Q8. What happens if an inheritance hierarchy violates C3 MRO consistency, such as class Z(X, Y) vs class Z2(Y, X) combined in class Bad(Z, Z2)?
(यदि class Z(X, Y) और class Z2(Y, X) को मिलाकर class Bad(Z, Z2) बनाया जाए जो C3 MRO नियमों के विरुद्ध है, तो क्या होगा?)
(A) TypeError: Cannot create a consistent method resolution order (MRO)
(B) SyntaxError occurs on the pass line
(C) RecursionError occurs only when calling a method
(D) Python silently picks whichever parent was defined first
Correct Answer: (A)
[EN] Conflicting local precedence orders violate C3 monotonicity and fail immediately at class creation with TypeError.
[HI] परस्पर विरोधी क्रम होने पर C3 MRO बनना असंभव होता है और क्लास बनते समय ही TypeError आ जाता है।
Q9. Which built-in syntax creates a class Child that inherits from both Father and Mother?
(Father और Mother दोनों से इनहेरिट होने वाली Child क्लास बनाने का सही पायथन सिंटैक्स क्या है?)
(A) class Child(Father & Mother):
(B) class Child(Father, Mother):
(C) class Child : Father + Mother:
(D) class Child extends Father, Mother:
Correct Answer: (B)
[EN] Python lists multiple base classes separated by commas inside parentheses after the class name.
[HI] पायथन में मल्टीपल इनहेरिटेंस के लिए क्लास के नाम के बाद कोष्ठक में कॉमा लगाकर बेस क्लासेस लिखी जाती हैं।
Q10. If Dog inherits from Animal, and Pug inherits from Dog, which type of inheritance is this?
(यदि Dog, Animal से इनहेरिट हो और Pug, Dog से इनहेरिट हो, तो यह किस प्रकार का इनहेरिटेंस है?)
(A) Hierarchical inheritance
(B) Multiple inheritance
(C) Multilevel inheritance
(D) Flat inheritance
Correct Answer: (C)
[EN] A vertical chain A -> B -> C across multiple levels is multilevel inheritance.
[HI] A -> B -> C जैसी क्रमिक पीढ़ीगत श्रृंखला को मल्टीलेवल इनहेरिटेंस कहा जाता है।
Q11. Which built-in function tests whether an object obj is an instance of class Cls or any subclass of Cls?
(कौन-सा बिल्ट-इन फंक्शन जांचता है कि ऑब्जेक्ट obj किसी क्लास Cls या उसके किसी सबक्लास का इंस्टेंस है?)
(A) type(obj) == Cls
(B) issubclass(obj, Cls)
(C) inherits(obj, Cls)
(D) isinstance(obj, Cls)
Correct Answer: (D)
[EN] Unlike type(obj) == Cls which ignores inheritance, isinstance(obj, Cls) honors subclass relationships.
[HI] type(obj) == Cls के विपरीत, isinstance(obj, Cls) इनहेरिटेंस पदानुक्रम को भी ध्यान में रखकर True देता है।
Q12. What happens in Python 3 if you call super().show() inside a normal method without passing (CurrentClass, self)?
(पायथन 3 में बिना (CurrentClass, self) लिखे सीधे super().show() उपयोग करने पर क्या होता है?)
(A) It raises TypeError requiring two arguments
(B) It calls the child class instead of parent
(C) It works only inside __init__ and fails in regular methods
(D) It works automatically because Python 3 injects __class__ and the first parameter
Correct Answer: (D)
[EN] PEP 3135 introduced zero-argument super() in Python 3 using compiler-injected __class__ closure cells.
[HI] पायथन 3 में जीरो-आर्ग्यूमेंट super() स्वतः __class__ और self को पहचानकर कार्य करता है।
Q13. What is the primary purpose of a Mixin class in Python multiple inheritance?
(पायथन मल्टीपल इनहेरिटेंस में Mixin क्लास का मुख्य उद्देश्य क्या होता है?)
(A) To inject a focused set of reusable methods into unrelated classes without deep inheritance trees
(B) To prevent subclasses from defining __init__
(C) To replace the root object class
(D) To act as an unbreakable abstract compiler interface
Correct Answer: (A)
[EN] Mixins bundle orthogonal features like serialization or logging to be mixed into classes via multiple inheritance.
[HI] मिक्सिन क्लासेस बिना गहरे पदानुक्रम के विभिन्न क्लासेस में लॉगिंग या सीरियलाइजेशन जैसे स्वतंत्र गुण जोड़ने हेतु उपयोग की जाती हैं।
Q14. Given class A: pass; class B(A): pass; class C(A): pass; class D(B, C): pass, what is the full tuple of class names in D.__mro__?
(class A, B(A), C(A), D(B, C) के लिए D.__mro__ में क्लासेस का सही क्रम क्या होगा?)
(A) (D, C, B, A, object)
(B) (D, B, A, C, object)
(C) (D, B, C, A, object)
(D) (A, B, C, D, object)
Correct Answer: (C)
[EN] C3 Linearization keeps B before C and ensures shared ancestor A is visited only after both B and C.
[HI] C3 लीनियराइजेशन B को C से पहले रखता है और साझा पूर्वज A को B व C दोनों के बाद (D, B, C, A, object) क्रम में रखता है।
Q15. Can a Python class directly inherit from a built-in container type such as list or dict?
(क्या कोई पायथन क्लास list या dict जैसे बिल्ट-इन कंटेनर टाइप से सीधे इनहेरिट हो सकती है?)
(A) Yes, built-in types like list and dict can be subclassed directly
(B) Only via collections.UserDict, never directly
(C) Only in Python 2
(D) No, built-in types are final and raise TypeError
Correct Answer: (A)
[EN] In Python 3, unified types allow direct subclassing of int, str, list, dict, and set (except special singletons like bool/NoneType).
[HI] पायथन 3 में list, dict, int और str जैसे बिल्ट-इन टाइप्स को सीधे सबक्लास किया जा सकता है।
Q16. Which statement about Python inheritance and MRO is NOT true?
(पायथन इनहेरिटेंस और MRO के बारे में निम्नलिखित में से कौन-सा कथन असत्य है?)
(A) Zero-argument super() is supported in Python 3
(B) Python resolves multiple inheritance strictly by naive depth-first search visiting A twice in a diamond
(C) isinstance(obj, (A, B)) accepts a tuple of candidate classes
(D) C3 Linearization guarantees monotonic ancestor ordering
Correct Answer: (B)
[EN] Naive depth-first search was Python 2's legacy old-style class behavior; Python 3 always uses C3 Linearization, visiting A once.
[HI] डायमंड में A को दो बार विजिट करना पुरानी डेप्थ-फर्स्ट खोज की समस्या थी; पायथन 3 में C3 MRO हमेशा A को एक ही बार विजिट करता है।
Q17. Within Inheritance in Python: Single, Multiple, Multilevel, super() Function, which inheritance feature directly maps to: "class Child(Parent):"?
(Inheritance in Python: Single, Multiple, Multilevel, super() Function में कौन सा Inheritance Feature सीधे "class Child(Parent):" से संबंधित है?)
(A) Single Inheritance
(B) Multiple Inheritance
(C) Multilevel Inheritance
(D) C3 MRO & super()
Correct Answer: (A)
[EN] In Inheritance in Python: Single, Multiple, Multilevel, super() Function, 'Single Inheritance' handles 'class Child(Parent):' (One child inherits one parent class).
[HI] Inheritance in Python: Single, Multiple, Multilevel, super() Function में 'Single Inheritance' का कार्य/विवरण 'class Child(Parent):' (One child inherits one parent class) है।
Q18. Which option accurately describes 'Multiple Inheritance' in Inheritance in Python: Single, Multiple, Multilevel, super() Function?
(Inheritance in Python: Single, Multiple, Multilevel, super() Function के संदर्भ में 'Multiple Inheritance' की सही विशेषता कौन सी है?)
(A) class D(B, C):
(B) class C(B): with class B(A):
(C) class Child(Parent):
(D) Cls.__mro__ / Cls.mro()
Correct Answer: (A)
[EN] 'Multiple Inheritance' provides 'class D(B, C):' — key point: Left-to-right base order guides MRO.
[HI] 'Multiple Inheritance' का मुख्य विवरण 'class D(B, C):' है (Left-to-right base order guides MRO)।
Q19. In Inheritance in Python: Single, Multiple, Multilevel, super() Function, identify the correct parameter for the specification: "class C(B): with class B(A):".
(Inheritance in Python: Single, Multiple, Multilevel, super() Function में "class C(B): with class B(A):" विवरण किस विकल्प के लिए सही है?)
(A) Multilevel Inheritance
(B) Single Inheritance
(C) Multiple Inheritance
(D) C3 MRO & super()
Correct Answer: (A)
[EN] 'Multilevel Inheritance' corresponds to 'class C(B): with class B(A):' (Sequential chain across generations).
[HI] 'Multilevel Inheritance' का संबंध 'class C(B): with class B(A):' (Sequential chain across generations) से है।
Q20. Considering Inheritance in Python: Single, Multiple, Multilevel, super() Function, what key behavior defines 'C3 MRO & super()'?
(Inheritance in Python: Single, Multiple, Multilevel, super() Function में 'C3 MRO & super()' की मुख्य भूमिका या मान क्या है?)
(A) Cls.__mro__ / Cls.mro()
(B) class D(B, C):
(C) class Child(Parent):
(D) class C(B): with class B(A):
Correct Answer: (A)
[EN] 'C3 MRO & super()' represents 'Cls.__mro__ / Cls.mro()' (Delegates to next MRO class, not blind parent).
[HI] 'C3 MRO & super()' का अर्थ/मान 'Cls.__mro__ / Cls.mro()' है (Delegates to next MRO class, not blind parent)।
Inheritance in Python: Single, Multiple, Multilevel, super() Function — Exam FAQs
Q1: How does Python solve the Diamond Problem in multiple inheritance?
पायथन मल्टीपल इनहेरिटेंस की डायमंड समस्या को कैसे हल करता है?
[EN] Python uses C3 Linearization to flatten the inheritance graph into a single unambiguous Method Resolution Order (MRO) where every ancestor appears once, combined with cooperative super() calls.
[HI] पायथन C3 लीनियराइजेशन एल्गोरिदम से पदानुक्रम को एक स्पष्ट MRO क्रम में बदलता है जहाँ प्रत्येक पूर्वज क्लास केवल एक बार आती है, और super() इसी क्रम में आगे बढ़ता है।
Q2: What is the difference between super().method() and ParentClass.method(self)?
super().method() और ParentClass.method(self) के बीच क्या अंतर है?
[EN] ParentClass.method(self) hardcodes a direct jump to ParentClass, which can break or duplicate calls in multiple inheritance, whereas super().method() dynamically delegates to the next class in the runtime MRO.
[HI] ParentClass.method(self) सीधे उसी पेरेंट क्लास पर जाता है जिससे डायमंड इनहेरिटेंस में डुप्लीकेट कॉल हो सकता है, जबकि super().method() रनटाइम MRO में अगली क्लास को चुनता है।
Q3: Why are some built-in types like bool impossible to subclass in Python?
पायथन में bool जैसे कुछ चुनिंदा टाइप्स को सबक्लास क्यों नहीं किया जा सकता?
[EN] Types like bool and NoneType are intentionally marked non-subclassable in CPython because language semantics rely on strictly two boolean singleton values (True and False).
[HI] पायथन भाषा की आंतरिक तर्क व्यवस्था केवल दो निश्चित बूलियन मानों (True और False) पर निर्भर करती है, इसलिए bool और NoneType को सबक्लास करने की अनुमति नहीं दी गई है।
Post a Comment
Post a Comment