PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम The global and nonlocal Keywords in Python (global और nonlocal कीवर्ड्स द्वारा बाहरी चरों में परिवर्तन) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: The global and nonlocal keywords are compile-time declarations that permit rebinding variables outside a function's local namespace. While global targets the module-level namespace, nonlocal (introduced in Python 3) targets the nearest enclosing function scope.
अध्याय सार: global और nonlocal ऐसे कम्पाइल-टाइम कीवर्ड्स हैं जो किसी फंक्शन के भीतर से बाहरी स्कोप के वेरिएबल्स को रीबाइंड (मान बदलने) की अनुमति देते हैं। जहां global मॉड्यूल स्तर के ग्लोबल नेमस्पेस को लक्षित करता है, वहीं nonlocal (पायथन 3 में जोड़ा गया) निकटतम एनक्लोजिंग फंक्शन स्कोप को लक्षित करता है।
The global and nonlocal Keywords in Python: Detailed Notes & Architecture (global और nonlocal कीवर्ड्स द्वारा बाहरी चरों में परिवर्तन)
1. How the global Keyword Rebinds Module Variables (global कीवर्ड द्वारा मॉड्यूल वेरिएबल्स में परिवर्तन)
[EN] Reading a module-level variable inside a function works automatically under LEGB without any keyword. However, assigning to that variable inside the function creates a new local variable unless you declare `global var_name`. The global declaration tells Python's compiler that every read and assignment of that identifier in the function refers directly to the module's global dictionary. Importantly, if the variable did not previously exist at module level, assigning to it after a `global` declaration creates a brand-new module-level global variable.
[HI] किसी फंक्शन के अंदर से ग्लोबल वेरिएबल को केवल पढ़ने के लिए किसी कीवर्ड की आवश्यकता नहीं होती। लेकिन यदि आप फंक्शन के भीतर उस वेरिएबल में नया मान असाइन करना चाहते हैं, तो `global var_name` लिखना अनिवार्य है, अन्यथा नया लोकल वेरिएबल बन जाता है। `global` कम्पाइलर को बताता है कि उस नाम के सभी संदर्भ सीधे मॉड्यूल की ग्लोबल डिक्शनरी में जाएंगे। यदि वह वेरिएबल मॉड्यूल स्तर पर पहले से मौजूद न भी हो, तो `global` के बाद असाइनमेंट करने पर वह मॉड्यूल स्तर पर नया ग्लोबल वेरिएबल बना देता है।
2. The nonlocal Keyword for Enclosing Closures (एनक्लोजिंग क्लोजर्स के लिए nonlocal कीवर्ड)
[EN] Introduced in Python 3.0, `nonlocal` allows an inner nested function to rebind a variable belonging to an outer enclosing function without touching the module global scope. Before Python 3, developers had to wrap values in a mutable list (e.g., `count = [0]`) to mutate closure state from inner functions. When multiple nesting levels exist, `nonlocal x` searches outward from the nearest enclosing function until it finds an existing binding for `x`, stopping strictly before module global scope. If no enclosing function defines `x`—even if `x` exists at the module global level—Python raises a compile-time `SyntaxError`.
[HI] पायथन 3.0 में प्रस्तुत `nonlocal` कीवर्ड किसी भीतरी नेस्टेड फंक्शन को अपने बाहरी एनक्लोजिंग फंक्शन के वेरिएबल को रीबाइंड करने की सुविधा देता है। पायथन 3 से पहले डेवलपर्स को क्लोजर में स्थिति बदलने के लिए `count = [0]` जैसी म्यूटेबल लिस्ट का सहारा लेना पड़ता था। अनेक नेस्टेड स्तरों में `nonlocal x` निकटतम बाहरी फंक्शन से खोज शुरू करता है और ग्लोबल स्कोप से ठीक पहले रुक जाता है। यदि किसी भी बाहरी फंक्शन में `x` मौजूद न हो (भले ही वह ग्लोबल स्कोप में हो), तो पायथन कम्पाइल समय पर `SyntaxError` दे देता है।
3. Compile-Time Parser Rules and SyntaxError Traps (कम्पाइल-टाइम नियम और SyntaxError स्थितियां)
[EN] Both `global` and `nonlocal` are compiler directives, not executable runtime statements, so placing them inside an `if False:` block still alters the function's symbol table. A variable cannot be read, assigned, or used as a function parameter in the same function before its `global` or `nonlocal` declaration; doing so triggers `SyntaxError`. You also cannot declare the same identifier as both `global` and `nonlocal` within the same scope. Furthermore, `nonlocal` cannot be used at the top level of a module or to bind a class attribute directly from inside a class method.
[HI] `global` और `nonlocal` रनटाइम स्टेटमेंट नहीं बल्कि कम्पाइलर निर्देश हैं, इसलिए ये पूरे फंक्शन की सिंबल टेबल को प्रभावित करते हैं। किसी वेरिएबल को `global` या `nonlocal` घोषित करने से पहले उसी फंक्शन में उपयोग करना या पैरामीटर बनाना `SyntaxError` उत्पन्न करता है। एक ही फंक्शन में किसी वेरिएबल को `global` और `nonlocal` दोनों घोषित नहीं किया जा सकता। इसके अतिरिक्त, मॉड्यूल के टॉप-लेवल पर या क्लास एट्रिब्यूट को सीधे बाइंड करने के लिए `nonlocal` का उपयोग करने पर भी `SyntaxError` आता है।
4. In-Place Mutation vs Rebinding and Design Trade-offs (म्यूटेशन बनाम रीबाइंडिंग और डिज़ाइन सावधानियां)
[EN] It is essential to distinguish between rebinding a name (`nums = [1, 2]`) and mutating an object in place (`nums.append(3)`). In-place mutation of a global or enclosing mutable object like a list or dictionary only performs a name lookup (read), so neither `global` nor `nonlocal` is required. Only rebinding the variable name with `=`, `+=` (on immutables like `int`/`str`), or `del` requires the scope declaration. In production architectures, heavy use of `global` creates hidden state dependencies; passing parameters, returning values, or using classes is preferred.
[HI] किसी नाम को रीबाइंड करने (`nums = [1, 2]`) और किसी म्यूटेबल ऑब्जेक्ट को इन-प्लेस बदलने (`nums.append(3)`) के बीच अंतर समझना आवश्यक है। ग्लोबल या बाहरी लिस्ट या डिक्शनरी में `append()` या की-अपडेट करने के लिए `global` या `nonlocal` की जरूरत नहीं होती क्योंकि वहां वेरिएबल नाम केवल पढ़ा जाता है। केवल `=`, इम्यूटेबल पर `+=` या `del` द्वारा नाम को नया ऑब्जेक्ट देने पर ही कीवर्ड जरूरी होता है। व्यावसायिक कोड में `global` का अत्यधिक उपयोग कोड को जटिल बनाता है, इसलिए रिटर्न वैल्यू या क्लास का उपयोग बेहतर माना जाता है।
Syntax, Formula & Technical Example (The global and nonlocal Keywords in Python)
calls = 0
def make_counter(start=0):
count = start
def step():
global calls
nonlocal count
calls += 1; count += 1
return count
return step
c = make_counter(10); print(c(), c(), "calls:", calls)
The global and nonlocal Keywords in Python Quick Reference & Comparison Table
| Keyword / Case | Target Scope & Precondition | Key Exam Fact |
|---|---|---|
| global | Module global namespace; name may be new | Creates global variable if not yet defined at module level |
| nonlocal | Nearest outer def scope (Python 3+) | SyntaxError if variable missing in enclosing functions |
| In-place Mutation | Modifies list/dict content via method | Does NOT require global or nonlocal declaration |
| Pre-use Violation | SyntaxError during bytecode compile | Triggered if variable used or passed as param before declaration |
The global and nonlocal Keywords in Python Top 20 Bilingual MCQs with Answers
Q1. Which scope does the `nonlocal` keyword bind a variable to?
(`nonlocal` कीवर्ड किसी वेरिएबल को किस स्कोप से जोड़ता है?)
(A) The nearest enclosing function scope (excluding module global scope)
(B) The enclosing class attribute dictionary
(C) The builtins module namespace
(D) The module-level global scope
Correct Answer: (A)
[EN] nonlocal walks outward through enclosing function scopes only and stops before the module global scope.
[HI] nonlocal केवल बाहरी नेस्टेड फंक्शन्स के स्कोप में खोज करता है और मॉड्यूल ग्लोबल स्कोप से पहले रुक जाता है।
Q2. In which major version of Python was the `nonlocal` keyword introduced?
(पायथन के किस प्रमुख संस्करण में `nonlocal` कीवर्ड को जोड़ा गया था?)
(A) Python 3.0
(B) Python 2.7
(C) Python 3.8
(D) Python 2.0
Correct Answer: (A)
[EN] The nonlocal statement was introduced in Python 3.0 via PEP 3104.
[HI] PEP 3104 के तहत Python 3.0 में nonlocal कीवर्ड जोड़ा गया था।
Q3. What happens if `global z` is used inside a function and `z = 100` is executed when `z` was never defined outside the function?
(यदि `z` फंक्शन के बाहर परिभाषित नहीं है और फंक्शन के अंदर `global z; z = 100` चलाया जाए, तो क्या होगा?)
(A) NameError is raised at runtime
(B) UnboundLocalError is raised
(C) SyntaxError: no binding for global z found
(D) A new global variable z with value 100 is created in the module namespace
Correct Answer: (D)
[EN] Unlike nonlocal, global allows creating a brand-new module-level variable if one did not exist prior to the function call.
[HI] nonlocal के विपरीत, global के साथ नया ग्लोबल वेरिएबल मॉड्यूल स्तर पर बनाया जा सकता है।
Q4. What happens if `nonlocal z` is declared in an inner function when `z` only exists at the module global level (not in any outer function)?
(यदि `z` केवल मॉड्यूल के ग्लोबल स्तर पर है और किसी बाहरी फंक्शन में नहीं, तो भीतरी फंक्शन में `nonlocal z` लिखने पर क्या होगा?)
(A) It successfully modifies the global variable z
(B) SyntaxError: no binding for nonlocal 'z' found
(C) It raises NameError at runtime when called
(D) It creates a new variable z in the outer function
Correct Answer: (B)
[EN] nonlocal strictly ignores module global variables and raises a SyntaxError at compile time if no enclosing function defines the name.
[HI] nonlocal ग्लोबल स्कोप को अनदेखा करता है और बाहरी फंक्शन में वेरिएबल न मिलने पर कम्पाइल समय पर SyntaxError देता है।
Q5. Inside a function, you execute `items.append(99)` on a globally defined list `items = [1, 2]`. Do you need `global items`?
(फंक्शन के भीतर ग्लोबल लिस्ट `items = [1, 2]` पर `items.append(99)` चलाने के लिए क्या `global items` लिखना जरूरी है?)
(A) No, because append() mutates the existing list without rebinding the name items
(B) Yes, otherwise a local copy is modified
(C) Yes, otherwise UnboundLocalError occurs
(D) No, only if items has fewer than 10 elements
Correct Answer: (A)
[EN] Mutating a list via .append() only reads the reference `items`, so LEGB finds it in Global scope without needing `global`.
[HI] append() केवल लिस्ट ऑब्जेक्ट में बदलाव करता है और वेरिएबल को रीबाइंड नहीं करता, इसलिए global की आवश्यकता नहीं है।
Q6. Inside a function, you want to replace the global list `items = [1, 2]` with `items = [3, 4]`. Is `global items` required?
(यदि आप फंक्शन के भीतर ग्लोबल लिस्ट `items = [1, 2]` को `items = [3, 4]` से बदलना चाहते हैं, तो क्या `global items` आवश्यक है?)
(A) No, Python automatically updates global lists
(B) No, lists are mutable so global is never needed
(C) Only if the new list is empty
(D) Yes, because assignment (=) rebinds the variable name
Correct Answer: (D)
[EN] Using `=` rebinds the identifier `items`. Without `global items`, Python simply creates a new local variable `items`.
[HI] `=` ऑपरेटर वेरिएबल नाम को नए ऑब्जेक्ट से जोड़ता है; `global items` के बिना केवल लोकल वेरिएबल बनेगा।
Q7. What happens if you write `print(x)` before `global x` inside the same function?
(यदि किसी फंक्शन के अंदर `global x` से पहले `print(x)` लिखा जाए तो क्या होगा?)
(A) It prints None without error
(B) SyntaxError: name 'x' is used prior to global declaration
(C) UnboundLocalError at runtime
(D) It prints the old global x then rebinds it
Correct Answer: (B)
[EN] Python forbids referencing or assigning a variable in a function body prior to its global or nonlocal declaration.
[HI] एक ही फंक्शन में global या nonlocal घोषणा से पहले उस वेरिएबल का उपयोग करना SyntaxError उत्पन्न करता है।
Q8. What happens when a function declares a parameter `x` and also includes `nonlocal x` inside its body?
(यदि कोई फंक्शन `x` को पैरामीटर के रूप में ले और अंदर `nonlocal x` लिखे तो क्या होगा?)
(A) The outer x is overwritten by the passed argument
(B) SyntaxError: name 'x' is parameter and nonlocal
(C) The parameter shadows the outer x silently
(D) TypeError is raised when calling the function
Correct Answer: (B)
[EN] Function parameters are inherently local bindings and cannot simultaneously be declared nonlocal or global.
[HI] फंक्शन पैरामीटर अनिवार्य रूप से लोकल होते हैं, उन्हें nonlocal या global घोषित करना SyntaxError देता है।
Q9. What is the output of: x = 1 def f(): global x x += 4 f() print(x)
(कोड का आउटपुट क्या होगा: x = 1 def f(): global x x += 4 f() print(x))
(A) UnboundLocalError
(B) NameError
(C) 5
(D) 1
Correct Answer: (C)
[EN] With `global x`, `x += 4` modifies the module-level variable `x` from 1 to 5.
[HI] `global x` के कारण `x += 4` सीधे ग्लोबल `x` को 1 से बढ़ाकर 5 कर देता है।
Q10. What is the output of: def outer(): n = 10 def inner(): nonlocal n n += 5 inner() return n print(outer())
(कोड का आउटपुट क्या होगा: def outer(): n = 10 def inner(): nonlocal n n += 5 inner() return n print(outer()))
(A) 15
(B) 10
(C) SyntaxError
(D) UnboundLocalError
Correct Answer: (A)
[EN] inner() uses `nonlocal n` to update outer()'s local variable `n` from 10 to 15.
[HI] inner() में `nonlocal n` के कारण outer() का वेरिएबल `n` 10 से बदलकर 15 हो जाता है।
Q11. In a 3-level nested function (outer -> middle -> inner) where both `outer` and `middle` define `x = 1` and `x = 2`, which `x` does `nonlocal x` inside `inner` modify?
(तीन-स्तरीय नेस्टेड फंक्शन (outer -> middle -> inner) में यदि outer में `x=1` और middle में `x=2` हो, तो inner में `nonlocal x` किसे बदलेगा?)
(A) SyntaxError due to ambiguity
(B) outer's x (outermost function)
(C) Both outer's x and middle's x
(D) middle's x (nearest enclosing function)
Correct Answer: (D)
[EN] nonlocal always binds to the nearest enclosing function scope that defines the identifier, which is middle.
[HI] nonlocal हमेशा उस नाम को परिभाषित करने वाले निकटतम बाहरी फंक्शन (middle) को लक्षित करता है।
Q12. What occurs if `nonlocal x` is written at module top-level outside any function or class?
(यदि किसी फंक्शन के बाहर सीधे मॉड्यूल के टॉप-लेवल पर `nonlocal x` लिखा जाए तो क्या होगा?)
(A) Nothing happens; it acts as a no-op
(B) NameError at runtime
(C) x becomes a global variable
(D) SyntaxError: nonlocal declaration not allowed at module level
Correct Answer: (D)
[EN] At module level there are no enclosing function scopes, making `nonlocal` syntactically invalid.
[HI] मॉड्यूल के टॉप-लेवल पर कोई बाहरी फंक्शन स्कोप नहीं होता, इसलिए वहां `nonlocal` लिखने पर SyntaxError आता है।
Q13. Can a single function declare `global a, b, c` in one statement?
(क्या एक ही स्टेटमेंट में `global a, b, c` लिखकर एक साथ कई वेरिएबल घोषित किए जा सकते हैं?)
(A) Only for global, not for nonlocal
(B) Yes, comma-separated identifiers are valid for both global and nonlocal
(C) Only up to two variables per statement
(D) No, each variable needs its own separate global line
Correct Answer: (B)
[EN] Both `global a, b` and `nonlocal x, y` accept comma-separated lists of identifiers.
[HI] हां, `global` और `nonlocal` दोनों में अल्पविराम (comma) से अलग करके कई नाम एक साथ लिखे जा सकते हैं।
Q14. Can `nonlocal` inside a class method rebind a class attribute defined directly in the class body?
(क्या क्लास के किसी मेथड के भीतर `nonlocal` लिखकर सीधे क्लास बॉडी में बने एट्रिब्यूट को बदला जा सकता है?)
(A) Yes, class body always acts as enclosing scope
(B) No, because a class body is not an enclosing function scope
(C) Only inside __init__
(D) Yes, for integer class attributes only
Correct Answer: (B)
[EN] Class namespaces are excluded from closure/nonlocal binding, raising SyntaxError if no outer function defines the name.
[HI] क्लास का नेमस्पेस एनक्लोजिंग फंक्शन स्कोप नहीं होता, इसलिए मेथड में `nonlocal` क्लास एट्रिब्यूट को नहीं पकड़ सकता।
Q15. Why was `count = [0]` commonly used inside Python 2 closures?
(पायथन 2 के क्लोजर्स में अक्सर `count = [0]` का उपयोग क्यों किया जाता था?)
(A) Integers could not be returned from inner functions in Python 2
(B) Python 2 lacked the nonlocal keyword, so mutating index [0] bypassed rebinding
(C) Lists run faster than integers in Python 2
(D) global keyword did not exist in Python 2
Correct Answer: (B)
[EN] Without nonlocal in Python 2, wrapping state inside a mutable list allowed inner functions to call `count[0] += 1` without rebinding `count`.
[HI] पायथन 2 में nonlocal नहीं था, इसलिए `count[0] += 1` जैसे म्यूटेबल लिस्ट अपडेट से काम चलाया जाता था।
Q16. Which statement is NOT true regarding `global` and `nonlocal`?
(`global` और `nonlocal` के विषय में निम्न में से कौन-सा कथन सत्य नहीं है?)
(A) Declaring the same variable both global and nonlocal in one function raises SyntaxError
(B) Reading a global variable inside a function does not require the global keyword
(C) global can introduce a new variable in the module namespace
(D) nonlocal automatically targets the global variable if no enclosing function defines the name
Correct Answer: (D)
[EN] nonlocal never falls back to the module global namespace; if no enclosing function contains the variable, Python raises SyntaxError.
[HI] यदि बाहरी फंक्शन में वेरिएबल नहीं है तो nonlocal कभी भी ग्लोबल स्कोप को नहीं पकड़ता, बल्कि SyntaxError देता है।
Q17. Within The global and nonlocal Keywords in Python, which keyword / case directly maps to: "Module global namespace; name may be new"?
(The global and nonlocal Keywords in Python में कौन सा Keyword / Case सीधे "Module global namespace; name may be new" से संबंधित है?)
(A) nonlocal
(B) global
(C) Pre-use Violation
(D) In-place Mutation
Correct Answer: (B)
[EN] In The global and nonlocal Keywords in Python, 'global' handles 'Module global namespace; name may be new' (Creates global variable if not yet defined at module level).
[HI] The global and nonlocal Keywords in Python में 'global' का कार्य/विवरण 'Module global namespace; name may be new' (Creates global variable if not yet defined at module level) है।
Q18. Which option accurately describes 'nonlocal' in The global and nonlocal Keywords in Python?
(The global and nonlocal Keywords in Python के संदर्भ में 'nonlocal' की सही विशेषता कौन सी है?)
(A) Module global namespace; name may be new
(B) SyntaxError during bytecode compile
(C) Nearest outer def scope (Python 3+)
(D) Modifies list/dict content via method
Correct Answer: (C)
[EN] 'nonlocal' provides 'Nearest outer def scope (Python 3+)' — key point: SyntaxError if variable missing in enclosing functions.
[HI] 'nonlocal' का मुख्य विवरण 'Nearest outer def scope (Python 3+)' है (SyntaxError if variable missing in enclosing functions)।
Q19. In The global and nonlocal Keywords in Python, identify the correct parameter for the specification: "Modifies list/dict content via method".
(The global and nonlocal Keywords in Python में "Modifies list/dict content via method" विवरण किस विकल्प के लिए सही है?)
(A) In-place Mutation
(B) global
(C) nonlocal
(D) Pre-use Violation
Correct Answer: (A)
[EN] 'In-place Mutation' corresponds to 'Modifies list/dict content via method' (Does NOT require global or nonlocal declaration).
[HI] 'In-place Mutation' का संबंध 'Modifies list/dict content via method' (Does NOT require global or nonlocal declaration) से है।
Q20. Considering The global and nonlocal Keywords in Python, what key behavior defines 'Pre-use Violation'?
(The global and nonlocal Keywords in Python में 'Pre-use Violation' की मुख्य भूमिका या मान क्या है?)
(A) Module global namespace; name may be new
(B) Modifies list/dict content via method
(C) Nearest outer def scope (Python 3+)
(D) SyntaxError during bytecode compile
Correct Answer: (D)
[EN] 'Pre-use Violation' represents 'SyntaxError during bytecode compile' (Triggered if variable used or passed as param before declaration).
[HI] 'Pre-use Violation' का अर्थ/मान 'SyntaxError during bytecode compile' है (Triggered if variable used or passed as param before declaration)।
The global and nonlocal Keywords in Python — Exam FAQs
Q1: Why do we not need `global` to append to a global list inside a function, whereas reassigning the list requires it?
फंक्शन के भीतर ग्लोबल लिस्ट में `append()` करने के लिए `global` की जरूरत क्यों नहीं होती, जबकि नई लिस्ट असाइन करने पर होती है?
[EN] Calling `my_list.append(5)` only looks up the existing list object and mutates its internal contents, without changing which object the name `my_list` points to. Writing `my_list = [5]` rebinds the name itself, which Python treats as creating a local variable unless `global my_list` is declared.
[HI] `my_list.append(5)` केवल मौजूदा लिस्ट ऑब्जेक्ट को ढूंढकर उसके भीतर डेटा जोड़ता है, नाम की बाइंडिंग नहीं बदलता। इसके विपरीत `my_list = [5]` नाम को नए ऑब्जेक्ट से जोड़ता है, जिसे पायथन `global` के बिना लोकल मान लेता है।
Q2: If a three-level nested function has `x` in the outermost function and not in the middle function, does `nonlocal x` in the innermost function still work?
यदि तीन नेस्टेड फंक्शन्स में `x` सबसे बाहरी फंक्शन में हो और बीच वाले में न हो, तो क्या सबसे भीतरी फंक्शन में `nonlocal x` काम करेगा?
[EN] Yes. `nonlocal` searches outward across all enclosing function scopes in order, skipping any intermediate function that does not define `x`, and binds to the outermost function's `x`.
[HI] हां, `nonlocal` क्रमिक रूप से सभी बाहरी फंक्शन स्कोप्स में खोज करता है और बीच वाले फंक्शन में न मिलने पर उससे बाहर के फंक्शन के `x` से जुड़ जाता है।
Q3: What happens if you assign `x = 10` inside `if False:` after reading `x` in a function?
यदि किसी फंक्शन में `x` को पढ़ने के बाद `if False: x = 10` लिखा हो, तो क्या होगा?
[EN] Even though `if False:` never executes at runtime, Python detects the assignment to `x` at compile time and marks `x` as a local variable for the whole function, raising UnboundLocalError when `x` is read earlier.
[HI] भले ही `if False:` कभी न चले, कम्पाइलर असाइनमेंट देखकर पूरे फंक्शन में `x` को लोकल मान लेता है, जिससे पहले पढ़ने पर UnboundLocalError आ जाता है।
Post a Comment
Post a Comment