🚀 Hurry! Offer Ends In
00 Days
00 Hours
00 Mins
00 Secs
Enroll Now
X

Python में किसी variable (identifier) को खोजने का क्रम क्या है?

What is the order in which Python searches for an identifier?

A
Local → Global → Enclosing → Built-in
B
Local → Enclosing → Global → Built-in
C
Built-in → Global → Enclosing → Local
D
Global → Local → Enclosing → Built-in
Explanation

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

Latest Updates