PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम The __init__() Constructor and self Parameter in Python (__init__() कंस्ट्रक्टर और 'self' पैरामीटर की भूमिका) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: In Python, __init__() is the special initializer method automatically invoked right after a new object is allocated to set up its initial attributes. Its mandatory first parameter, conventionally named self, binds to the newly created instance so each object maintains independent state.
अध्याय सार: पायथन में __init__() एक विशेष इनिशियलाइज़र मेथड है जो नया ऑब्जेक्ट मेमोरी में बनने के तुरंत बाद उसके शुरुआती एट्रिब्यूट्स सेट करने के लिए स्वतः कॉल होता है। इसका पहला पैरामीटर परंपरागत रूप से self कहलाता है, जो वर्तमान ऑब्जेक्ट का रेफरेंस ग्रहण करता है ताकि प्रत्येक इंस्टेंस अपना स्वतंत्र डेटा रख सके।
The __init__() Constructor and self Parameter in Python: Detailed Notes & Architecture (__init__() कंस्ट्रक्टर और 'self' पैरामीटर की भूमिका)
1. Object Lifecycle: __new__() vs __init__() (ऑब्जेक्ट जीवनचक्र: __new__() बनाम __init__())
[EN] Object instantiation happens in two distinct phases in Python. First, the static method __new__(cls, *args) allocates memory and returns the new instance; second, __init__(self, *args) initializes attributes on that already allocated object. Because __init__() only configures an existing object, it must return None—explicitly returning any non-None value from __init__() raises a TypeError at instantiation time.
[HI] पायथन में ऑब्जेक्ट निर्माण दो चरणों में होता है: पहले स्टैटिक मेथड __new__(cls, *args) मेमोरी आवंटित करके नया इंस्टेंस लौटाता है, और फिर __init__(self, *args) उस बने हुए ऑब्जेक्ट के एट्रिब्यूट्स सेट करता है। चूंकि __init__() केवल इनिशियलाइज़र है, इसलिए इसका रिटर्न मान हमेशा None होना चाहिए; कोई अन्य मान लौटाने पर TypeError उत्पन्न होता है।
2. Mechanics of the self Parameter (self पैरामीटर की आंतरिक कार्यप्रणाली)
[EN] When you call acc.deposit(250), Python's method descriptor translates it internally into Account.deposit(acc, 250), automatically passing the instance as the first argument. Although self is not a reserved keyword—meaning any identifier like this or obj technically works—PEP 8 strictly mandates self as the universal convention. Omitting self from an instance method header triggers a TypeError complaining that an extra positional argument was supplied.
[HI] जब आप acc.deposit(250) लिखते हैं, तो पायथन का डिस्क्रिप्टर प्रोटोकॉल इसे आंतरिक रूप से Account.deposit(acc, 250) में बदल देता है और इंस्टेंस को स्वतः पहले आर्ग्यूमेंट के रूप में भेजता है। ध्यान रहे कि self कोई आरक्षित कीवर्ड नहीं है—आप इसकी जगह कोई भी नाम लिख सकते हैं, परंतु PEP 8 के अनुसार self मानक परंपरा है। यदि मेथड परिभाषा में self छूट जाए, तो कॉल करने पर अतिरिक्त पोजीशनल आर्ग्यूमेंट का TypeError आता है।
3. Parameterized, Default, and Alternative Initializers (पैरामीटराइज्ड और वैकल्पिक कंस्ट्रक्टर पैटर्न)
[EN] Python does not support multiple overloaded __init__ methods by parameter signature; writing two __init__ definitions in the same class simply causes the second to overwrite the first in the class dictionary. Flexible construction is instead achieved with default arguments, *args/**kwargs, or @classmethod factory constructors such as Date.from_iso(). Crucially, never use a mutable default like items=[] inside __init__, as every instance would share the same list; initialize items=None instead.
[HI] पायथन में जावा या C++ की तरह पैरामीटर संख्या के आधार पर __init__ ओवरलोडिंग नहीं होती; एक ही क्लास में दो __init__ लिखने पर बाद वाला पहले को ओवरराइट कर देता है। लचीले निर्माण के लिए डिफ़ॉल्ट पैरामीटर, *args/**kwargs या Date.from_iso() जैसे @classmethod फैक्ट्री मेथड उपयोग किए जाते हैं। साथ ही __init__ में items=[] जैसे म्यूटेबल डिफ़ॉल्ट से बचना चाहिए क्योंकि वह सभी ऑब्जेक्ट्स में साझा हो जाता है।
4. Destructor __del__() and super().__init__() Delegation (डिस्ट्रक्टर __del__() और super().__init__() कॉल)
[EN] When a subclass defines its own __init__(), it overrides the parent's initializer, so calling super().__init__(...) is essential to ensure inherited parent attributes are properly configured. At the end of an object's life, __del__(self) serves as a finalizer executed when the reference count reaches zero or the cyclic garbage collector reclaims it. Because __del__ timing is nondeterministic, context managers (__enter__/__exit__) are preferred for closing files and network sockets.
[HI] जब कोई सबक्लास अपना __init__() परिभाषित करती है, तो वह पेरेंट के इनिशियलाइज़र को ओवरराइड कर देती है, इसलिए पेरेंट के एट्रिब्यूट्स सेट करने हेतु super().__init__(...) कॉल करना आवश्यक होता है। ऑब्जेक्ट का रेफरेंस काउंट शून्य होने पर गार्बेज कलेक्टर __del__(self) को फाइनलाइज़र के रूप में चलाता है, किंतु इसका समय अनिश्चित होने के कारण फाइल या सॉकेट बंद करने के लिए with स्टेटमेंट (कॉन्टेक्स्ट मैनेजर) को प्राथमिकता दी जाती है।
Syntax, Formula & Technical Example (The __init__() Constructor and self Parameter in Python)
class Account:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
def deposit(self, amt):
self.balance += amt
return self.balance
acc = Account('Neha', 500)
print(acc.deposit(250))
The __init__() Constructor and self Parameter in Python Quick Reference & Comparison Table
| Lifecycle Method / Term | Execution Role | Exam Key Point |
|---|---|---|
| __new__(cls) | Allocates memory and returns new instance | Runs before __init__; needed for immutables |
| __init__(self) | Initializes attributes on allocated object | Must return None or raises TypeError |
| self parameter | Binds to current instance reference | PEP 8 convention, not a reserved keyword |
| __del__(self) | Finalizer invoked before memory reclamation | Execution timing can be non-deterministic |
The __init__() Constructor and self Parameter in Python Top 20 Bilingual MCQs with Answers
Q1. Which method is actually responsible for allocating memory and creating a new object instance before __init__ runs?
(__init__ चलने से पहले मेमोरी आवंटित करके नया ऑब्जेक्ट बनाने का वास्तविक कार्य कौन-सा मेथड करता है?)
(A) __create__()
(B) __alloc__()
(C) __new__()
(D) __construct__()
Correct Answer: (C)
[EN] __new__(cls, ...) is a static method that creates and returns the raw object, which is then passed as self to __init__.
[HI] __new__(cls, ...) वास्तविक ऑब्जेक्ट बनाकर लौटाता है, जिसे बाद में इनिशियलाइज़ करने के लिए __init__ को self के रूप में भेजा जाता है।
Q2. What happens if an __init__() method explicitly executes return 10?
(यदि __init__() मेथड के अंदर return 10 लिखा जाए तो ऑब्जेक्ट बनाते समय क्या होगा?)
(A) TypeError: __init__() should return None, not 'int'
(B) The variable receives the integer 10 instead of the object
(C) Python silently ignores the return value
(D) SyntaxError occurs during compilation
Correct Answer: (A)
[EN] Python strictly enforces that __init__() returns None; returning any other object triggers TypeError.
[HI] पायथन के नियम अनुसार __init__() को केवल None लौटाना आवश्यक है; अन्य मान लौटाने पर TypeError आता है।
Q3. Is self a reserved keyword in Python?
(क्या self पायथन में एक आरक्षित कीवर्ड (reserved keyword) है?)
(A) Yes, it is listed in keyword.kwlist
(B) No, it is a strong PEP 8 naming convention
(C) Only inside classes inheriting from object
(D) Yes, using any other identifier causes SyntaxError
Correct Answer: (B)
[EN] self is not in keyword.kwlist; any valid variable name works as the first parameter, though PEP 8 requires self by convention.
[HI] self कोई आरक्षित कीवर्ड नहीं है बल्कि PEP 8 द्वारा अनुशंसित मानक नामकरण परंपरा है।
Q4. What happens when you define two __init__ methods with different parameter counts in the same Python class?
(एक ही पायथन क्लास में अलग-अलग पैरामीटर संख्या के साथ दो __init__ मेथड परिभाषित करने पर क्या होता है?)
(A) The second __init__ definition overwrites the first one
(B) NameError is raised immediately
(C) Python selects the matching signature like C++ overloading
(D) Both constructors execute sequentially
Correct Answer: (A)
[EN] Because a class namespace is a dictionary, defining the same method name twice causes the later definition to replace the earlier one.
[HI] क्लास नेमस्पेस एक डिक्शनरी की तरह होता है, अतः एक ही नाम से दूसरा __init__ लिखने पर पहला वाला ओवरराइट हो जाता है।
Q5. Calling acc.deposit(250) on an Account object is equivalent to which explicit call?
(Account के ऑब्जेक्ट पर acc.deposit(250) कॉल करना किस स्पष्ट कॉल के समतुल्य है?)
(A) Account.init(acc, 250)
(B) Account.deposit(acc, 250)
(C) deposit(250)
(D) Account.deposit(250, acc)
Correct Answer: (B)
[EN] Method binding passes the instance acc automatically as the first positional argument corresponding to self.
[HI] मेथड बाइंडिंग के तहत ऑब्जेक्ट acc स्वतः पहले पोजीशनल आर्ग्यूमेंट (self) के रूप में पास होता है।
Q6. What error occurs if an instance method def show(): is defined without self and called via obj.show()?
(यदि किसी इंस्टेंस मेथड को बिना self के def show(): लिखकर obj.show() से कॉल किया जाए तो कौन-सी त्रुटि आएगी?)
(A) ValueError: missing receiver
(B) NameError: name 'self' is not defined
(C) AttributeError: object has no attribute 'show'
(D) TypeError: show() takes 0 positional arguments but 1 was given
Correct Answer: (D)
[EN] Python passes obj implicitly as 1 positional argument, failing with TypeError because show() declares 0 parameters.
[HI] पायथन obj को स्वतः 1 आर्ग्यूमेंट के रूप में भेजता है जबकि show() में 0 पैरामीटर हैं, जिससे TypeError आता है।
Q7. Why is def __init__(self, items=[]): considered a classic bug in Python?
(पायथन में def __init__(self, items=[]): लिखना एक बड़ी त्रुटि (bug) क्यों माना जाता है?)
(A) Lists cannot be passed as arguments to __init__
(B) It causes infinite recursion during object allocation
(C) The list becomes immutable inside the constructor
(D) The default list object is created once at definition time and shared across all instances
Correct Answer: (D)
[EN] Default parameter values are evaluated once when def runs, so every instance lacking an explicit list mutates the same list.
[HI] डिफ़ॉल्ट आर्ग्यूमेंट फंक्शन बनते समय केवल एक बार बनता है, जिससे बिना आर्ग्यूमेंट वाले सभी ऑब्जेक्ट एक ही लिस्ट साझा करते हैं।
Q8. Which pattern is Idiomatic in Python to offer alternative ways of constructing a class (such as from a string or dict)?
(किसी क्लास को विभिन्न रूपों (जैसे स्ट्रिंग या डिक्शनरी से) बनाने हेतु पायथन में कौन-सा तरीका मानक माना जाता है?)
(A) @classmethod factory methods calling cls(...)
(B) Declaring global constructors outside the class
(C) Multiple __init__ overloads with @overload only
(D) Modifying __del__ to accept parameters
Correct Answer: (A)
[EN] @classmethod constructors like Date.from_string() cleanly construct and return cls(...) instances without breaking __init__.
[HI] Date.from_string() जैसे @classmethod फैक्ट्री मेथड cls(...) को कॉल करके वैकल्पिक कंस्ट्रक्टर की तरह काम करते हैं।
Q9. What happens if a Python class does not define its own __init__() method?
(यदि किसी पायथन क्लास में स्वयं का __init__() मेथड परिभाषित न हो तो क्या होता है?)
(A) Objects cannot be instantiated from that class
(B) All attributes are automatically initialized to 0
(C) SyntaxError is raised on class creation
(D) Python uses the inherited __init__() from its superclass (or object)
Correct Answer: (D)
[EN] Classes inherit __init__ from their parent or root object class, allowing instantiation with no arguments.
[HI] ऐसी क्लास अपनी पेरेंट या object क्लास का डिफ़ॉल्ट __init__() उपयोग करती है और बिना आर्ग्यूमेंट के ऑब्जेक्ट बना सकती है।
Q10. What does the Account snippet above print?
(ऊपर दिए गए Account कोड स्निपेट को चलाने पर क्या प्रिंट होगा?)
(A) None
(B) 250
(C) 750
(D) 500
Correct Answer: (C)
[EN] Account('Neha', 500) sets self.balance to 500; deposit(250) adds 250 and returns 750.
[HI] Account('Neha', 500) से balance 500 सेट होता है और deposit(250) उसे 750 करके लौटाता है।
Q11. Which method serves as the destructor/finalizer in Python when an object is about to be garbage collected?
(पायथन में किसी ऑब्जेक्ट के गार्बेज कलेक्ट होने से पहले अंतिम सफाई के लिए कौन-सा मेथड चलता है?)
(A) __free__()
(B) __destroy__()
(C) __del__()
(D) __close__()
Correct Answer: (C)
[EN] __del__(self) is called when an object's reference count drops to zero and the garbage collector finalizes it.
[HI] जब किसी ऑब्जेक्ट के सभी रेफरेंस समाप्त हो जाते हैं, तब गार्बेज कलेक्टर __del__(self) मेथड को कॉल करता है।
Q12. How do you ensure a subclass's __init__() also initializes attributes defined in its parent class?
(यह कैसे सुनिश्चित किया जाता है कि सबक्लास का __init__() अपनी पेरेंट क्लास के एट्रिब्यूट्स को भी इनिशियलाइज़ करे?)
(A) Python always executes parent __init__ automatically even if overridden
(B) By calling super().__init__(...)
(C) By marking the child __init__ with @inherit
(D) By calling self.__new__() again
Correct Answer: (B)
[EN] Overriding __init__ suppresses automatic invocation of the parent __init__, so super().__init__() must be called explicitly.
[HI] सबक्लास में __init__ लिखने पर पेरेंट का __init__ स्वतः नहीं चलता, इसलिए super().__init__() को स्पष्ट रूप से कॉल करना पड़ता है।
Q13. Which statement regarding __new__ and __init__ is true for immutable subclasses like subclassing int or str?
(int या str जैसी इम्यूटेबल बिल्ट-इन क्लासेस को सबक्लास करते समय __new__ और __init__ के बारे में कौन-सा कथन सत्य है?)
(A) Values can only be modified inside __init__
(B) Value modification must happen in __new__ because the instance is already immutable by the time __init__ runs
(C) __init__ allocates a fresh copy replacing __new__
(D) Immutable classes prohibit overriding __new__
Correct Answer: (B)
[EN] Because int and str objects cannot be altered once created, __new__(cls, ...) must construct the altered immutable value.
[HI] int और str जैसे ऑब्जेक्ट बनने के बाद बदले नहीं जा सकते, अतः उनका मान __init__ से पहले __new__ में ही संशोधित करना होता है।
Q14. In Python, what happens when you call acc = Account('Neha') using the Account code above where balance=0 is default?
(ऊपर दिए गए Account कोड में यदि balance=0 डिफ़ॉल्ट हो और acc = Account('Neha') लिखा जाए तो क्या होगा?)
(A) TypeError because two arguments are required
(B) acc.balance becomes None
(C) acc.balance is initialized to 0
(D) AttributeError on balance access
Correct Answer: (C)
[EN] Default arguments in __init__ work exactly like standard Python functions, assigning 0 when balance is omitted.
[HI] __init__ में डिफ़ॉल्ट आर्ग्यूमेंट सामान्य फंक्शन की तरह कार्य करते हैं, अतः balance न देने पर 0 सेट हो जाता है।
Q15. Which statement about __init__ and self in Python is NOT true?
(पायथन में __init__ और self के बारे में निम्नलिखित में से कौन-सा कथन असत्य है?)
(A) self must be explicitly passed inside parentheses when calling obj.method()
(B) Each object receives its own instance reference via self
(C) Using this instead of self is syntactically valid though discouraged
(D) __init__ executes automatically whenever a class is instantiated
Correct Answer: (A)
[EN] Python passes self automatically on bound method calls like obj.method(); passing it manually would duplicate the argument.
[HI] obj.method() कॉल करते समय self को अलग से पास नहीं करना होता; पायथन उसे स्वतः भेजता है।
Q16. If a class defines __init__(self, x), how many arguments must be supplied when instantiating ClassName(...) directly?
(यदि किसी क्लास में __init__(self, x) परिभाषित है, तो ClassName(...) लिखते समय कितने आर्ग्यूमेंट देने होंगे?)
(A) 2 arguments (one for self and one for x)
(B) 3 arguments including cls
(C) 0 arguments because x is optional
(D) 1 argument (for x), because Python supplies self automatically
Correct Answer: (D)
[EN] Instantiation ClassName(val) allocates the object as self and forwards val to x.
[HI] ऑब्जेक्ट बनाते समय self स्वतः जुड़ जाता है, इसलिए यूजर को केवल x के लिए 1 आर्ग्यूमेंट देना होता है।
Q17. Within The __init__() Constructor and self Parameter in Python, which lifecycle method / term directly maps to: "Allocates memory and returns new instance"?
(The __init__() Constructor and self Parameter in Python में कौन सा Lifecycle Method / Term सीधे "Allocates memory and returns new instance" से संबंधित है?)
(A) self parameter
(B) __del__(self)
(C) __init__(self)
(D) __new__(cls)
Correct Answer: (D)
[EN] In The __init__() Constructor and self Parameter in Python, '__new__(cls)' handles 'Allocates memory and returns new instance' (Runs before __init__; needed for immutables).
[HI] The __init__() Constructor and self Parameter in Python में '__new__(cls)' का कार्य/विवरण 'Allocates memory and returns new instance' (Runs before __init__; needed for immutables) है।
Q18. Which option accurately describes '__init__(self)' in The __init__() Constructor and self Parameter in Python?
(The __init__() Constructor and self Parameter in Python के संदर्भ में '__init__(self)' की सही विशेषता कौन सी है?)
(A) Finalizer invoked before memory reclamation
(B) Allocates memory and returns new instance
(C) Binds to current instance reference
(D) Initializes attributes on allocated object
Correct Answer: (D)
[EN] '__init__(self)' provides 'Initializes attributes on allocated object' — key point: Must return None or raises TypeError.
[HI] '__init__(self)' का मुख्य विवरण 'Initializes attributes on allocated object' है (Must return None or raises TypeError)।
Q19. In The __init__() Constructor and self Parameter in Python, identify the correct parameter for the specification: "Binds to current instance reference".
(The __init__() Constructor and self Parameter in Python में "Binds to current instance reference" विवरण किस विकल्प के लिए सही है?)
(A) __del__(self)
(B) __new__(cls)
(C) __init__(self)
(D) self parameter
Correct Answer: (D)
[EN] 'self parameter' corresponds to 'Binds to current instance reference' (PEP 8 convention, not a reserved keyword).
[HI] 'self parameter' का संबंध 'Binds to current instance reference' (PEP 8 convention, not a reserved keyword) से है।
Q20. Considering The __init__() Constructor and self Parameter in Python, what key behavior defines '__del__(self)'?
(The __init__() Constructor and self Parameter in Python में '__del__(self)' की मुख्य भूमिका या मान क्या है?)
(A) Initializes attributes on allocated object
(B) Finalizer invoked before memory reclamation
(C) Allocates memory and returns new instance
(D) Binds to current instance reference
Correct Answer: (B)
[EN] '__del__(self)' represents 'Finalizer invoked before memory reclamation' (Execution timing can be non-deterministic).
[HI] '__del__(self)' का अर्थ/मान 'Finalizer invoked before memory reclamation' है (Execution timing can be non-deterministic)।
The __init__() Constructor and self Parameter in Python — Exam FAQs
Q1: Why is __init__ technically called an initializer rather than a constructor?
__init__ को तकनीकी रूप से कंस्ट्रक्टर के बजाय इनिशियलाइज़र क्यों कहा जाता है?
[EN] Because memory allocation and instance creation are already completed by __new__() before __init__() is invoked; __init__ merely sets initial attributes on the already constructed self object.
[HI] क्योंकि ऑब्जेक्ट के लिए मेमोरी आवंटित करने और उसे बनाने का काम __new__() पहले ही कर चुका होता है; __init__() केवल उस बने हुए ऑब्जेक्ट (self) में शुरुआती डेटा भरता है।
Q2: Can we replace self with another word like this in Python?
क्या हम पायथन में self के स्थान पर this या कोई अन्य शब्द उपयोग कर सकते हैं?
[EN] Technically yes, because self is a parameter name rather than a keyword. However, PEP 8 and all Python linters/IDEs expect self, so renaming it breaks readability and tooling conventions.
[HI] तकनीकी रूप से हाँ, क्योंकि self कोई आरक्षित कीवर्ड नहीं है। परंतु PEP 8 और सभी पायथन डेवलपर टूल्स self की अपेक्षा करते हैं, इसलिए हमेशा self ही लिखना चाहिए।
Q3: How do you mimic multiple constructors in Python without constructor overloading?
कंस्ट्रक्टर ओवरलोडिंग के बिना पायथन में मल्टीपल कंस्ट्रक्टर जैसा व्यवहार कैसे प्राप्त करें?
[EN] Use default parameter values in __init__, inspect optional parameters or *args, or define @classmethod factory methods such as User.from_json(data) that call cls(...) internally.
[HI] __init__ में डिफ़ॉल्ट पैरामीटर मान देकर, *args/**kwargs का उपयोग करके या User.from_json() जैसे @classmethod फैक्ट्री मेथड बनाकर मल्टीपल कंस्ट्रक्टर की सुविधा दी जाती है।
Post a Comment
Post a Comment