1.2.30 · HinglishIntroduction to Programming (Python)

Variable scope — LEGB rule (Local, Enclosing, Global, Built-in)

1,772 words8 min readRead in English

1.2.30 · Coding › Introduction to Programming (Python)


Scope exist kyun karta hai?

"Scope" kya hai? Code ka woh region jahan ek naam visible/usable hai. "Namespace" kya hai? Woh actual dictionary jo naam → objects ko map karti hai aur ek scope ko support karti hai.


Chaar scopes (woh rooms jinmein Python dhundta hai)

Figure — Variable scope — LEGB rule (Local, Enclosing, Global, Built-in)

Lookup kaise kaam karta hai — Python ki tarah trace karo


Crucial twist: assignment ek LOCAL naam banata hai

Fix karna: global aur nonlocal


Read vs. write — woh rule jo sab kuch unify karta hai

Operation Python kya karta hai
Read x L → E → G → B search karo, pehla hit jeetega
Write x = … (bina keyword) Hamesha Local banata/use karta hai
Write global x ke saath Global ko target karta hai
Write nonlocal x ke saath Enclosing ko target karta hai

Recall Feynman: ek 12-saal ke bacche ko samjhao

Socho tum poochh rahe ho "Mere joote kahan hain?" Tum pehle apna kamra check karte ho (Local), phir bade bhai ka kamra (Enclosing), phir darwaze ke paas ki jagah (Global), aur finally woh joote ki dukaan jo hamesha joote rakhti hai (Built-in). Tum pehli pair jo milti hai woh le lete ho aur dhundna band kar dete ho. Lekin ek twist hai: agar tum kabhi kehte ho "Main joote YAHAN apne kamre mein rakhunga" (shoes = ...), Python maanta hai poore din joote tumhare kamre mein hain — toh agar tum unhe rakhne se pehle pehanna chaho, tum naange pair khadé ho aur confused ho (yahi hai UnboundLocalError). global kehna aise hai jaise sabko batao "mera matlab darwaze wale joote hain, mere nahi."


Active Recall

LEGB stands for
Local, Enclosing, Global, Built-in — woh order jisme Python ek naam padhte waqt dhundta hai.
First-match rule in LEGB
Python PEHLA scope (L→E→G→B order mein) use karta hai jisme naam hota hai aur dhundna band kar deta hai.
Which scope is searched first
Local (current function ke khud ke assigned names).
Which scope is searched last
Built-in (jaise len, print, range, sum).
Why does x = x + 1 cause UnboundLocalError for a global x
Assignment x ko poore function mein local bana deta hai, toh assign hone se pehle x read karna fail ho jaata hai; Python global ko kabhi consult nahi karta.
What does global x do
Function ko batata hai ki x module-level (Global) naam ko refer karta hai, toh assignments global ko rebind karte hain.
What does nonlocal x do
x ko nearest ENCLOSING function ke variable se bind karta hai; assignments use edit karte hain, aur woh variable pehle se exist karna chahiye.
Difference between global and nonlocal
global module scope (G) ko target karta hai; nonlocal enclosing function scope (E) ko target karta hai.
Do for/if blocks create scope in Python
Nahi — sirf functions, modules, aur comprehensions scope banate hain; unke naam enclosing function/module scope mein jaate hain.
What is shadowing a built-in
L/E/G level par ek naam (jaise len) define karna taaki Python use built-in se pehle dhoondh le aur original chhup jaaye.
Reading vs writing rule
Reading L→E→G→B dhundti hai; writing (bina keyword) hamesha ek Local naam banata/use karta hai.

Connections

  • Functions and Parameters — scope banaya jaata hai function calls se.
  • Closures and Nested FunctionsEnclosing scope (E) par rely karte hain.
  • Namespaces and the dict model — scopes dictionaries se backed hain.
  • Mutable vs Immutable arguments — function ke andar list mutate karna ≠ use rebind karna.
  • NameError and UnboundLocalError — LEGB ke direct consequences.
  • Modules and importimport Global namespace ko populate karta hai.

Concept Map

backed by

solves

resolves

first match wins

no match

shadows

forces name to

read before assign

Scope: region where name visible

Namespace: dict name to object

Name collisions across functions

LEGB lookup order

Reading a name

Local: current function

Enclosing: outer function

Global: module top level

Built-in: len, print, range

Stops searching

NameError

Assignment name equals ...

UnboundLocalError