1.2.31 · HinglishIntroduction to Programming (Python)

global and nonlocal keywords

1,317 words6 min readRead in English

1.2.31 · Coding › Introduction to Programming (Python)


WHY do these keywords exist?

Ye rule HI poora reason hai kyun global/nonlocal exist karte hain. Aao dekhte hain kyun ye tumhe takleef deta hai.


The scope ladder (LEGB)

Reading ke liye naam lookup jaata hai: Local → Enclosing → Global → Built-in (LEGB).

  • global = "seedha G level pe jaao aur wahan bind karo."
  • nonlocal = "sabse kareeb wale E (enclosing function) level pe bind karo — na local, na global."
Figure — global and nonlocal keywords

global — WHAT, WHY, HOW


nonlocal — WHAT, WHY, HOW


Forecast-then-Verify

Recall Answer padhne se PEHLE output predict karo
x = "global"
def outer():
    x = "enclosing"
    def inner():
        nonlocal x
        x = "changed"
    inner()
    print(x)
outer()
print(x)

Answer: pehle changed phir global print hoga. Kyun? nonlocal x inner ki assignment ko outer ke x se bind karta hai, ise "changed" kar deta hai. Module-level x untouched rehta hai, toh wo "global" hi rehta hai.


Flashcards

Function ke andar assign kiye gaye naam ka default scope kya hota hai?
Us function ka local, poore function body ke liye (compile time pe decide hota hai).
Usi function mein assign karne se pehle naam read karne pe kaun sa error aata hai?
UnboundLocalError.
global x kya karta hai?
x ko module-level variable ko refer karne (aur rebind karne) deta hai, na ki local ko.
nonlocal x kya karta hai?
x ko nearest enclosing function scope (global nahi) se bind karta hai aur use rebind karne deta hai.
Kya outer variable sirf READ karne ke liye global/nonlocal chahiye?
Nahi — reading LEGB automatically climb karta hai; inhe sirf rebind/assign karne ke liye chahiye.
Kya global list pe mylist.append(5) karne ke liye global chahiye?
Nahi — ye object ko mutate karta hai, naam ko rebind nahi karta.
LEGB ka full form kya hai?
Local, Enclosing, Global, Built-in (naam lookup ka order).
Jab nonlocal variable nahi milta, toh kya hota hai?
SyntaxError — enclosing function scope mein pehle se ek binding honi chahiye.
global aur nonlocal targets mein kya fark hai?
global → module top level; nonlocal → nearest enclosing function.

Recall Feynman: 12-saal ke bachche ko samjhao

Ek ghar imagine karo jisme kamre ke andar kamre hain. Har kamre ka apna toy box ho sakta hai. Normally, agar tum kisi kamre mein khade hokar ek box mein toy rakhte ho, tum us kamre mein naya box banate ho. global kehta hai: "Naya box mat banao — front hall mein wala BADA box use karo, jo sabka shared hai." nonlocal kehta hai: "Is kamre ke thoda bahar wale kamre ka box use karo, front hall wala nahi." Agar tum sirf bahar ke box se toy dekhna chahte ho, kuch bolne ki zaroorat nahi — tum hamesha bahar jhank sakte ho.


Connections

  • Scope and LEGB rule
  • Functions in Python
  • Closures
  • Mutable vs Immutable objects
  • UnboundLocalError
  • Pure functions and side effects

Concept Map

makes name

reading needs

assign before value

fixed by

binds at

Local Enclosing Global Builtin

binds at

used inside

assign rebinds

assign rebinds

needs neither

Assignment rule

Local by default

LEGB lookup

UnboundLocalError

global keyword

Global module level

nonlocal keyword

Enclosing function level

Nested function closure

Outer variable changed

Pure reading or mutating