Python में किसी variable (identifier) को खोजने का क्रम क्या है?
What is the order in which Python searches for an identifier?
Python में जब किसी variable (identifier) को access किया जाता है, तो interpreter उसे LEGB rule के अनुसार खोजता है:
LEGB = Local → Enclosing → Global → Built-in
🔸 1. Local (L):
Current function/block के अंदर define हुआ variable
🔸 2. Enclosing (E):
अगर function किसी और function के अंदर है, तो outer function का scope
🔸 3. Global (G):
Module level (script level) पर defined variables
🔸 4. Built-in (B):
Python के inbuilt functions/objects (जैसे len, print, sum)
🔹 Example:
x = "global"
def outer():
x = "enclosing"
def inner():
x = "local"
print(x) # Searches: Local → Enclosing → Global → Built-in
inner()
outer()
Output: local
Correct Answer: B) Local → Enclosing → Global → Built-in