LEGB Rule in Python: पायथन में LEGB नियम क्या है?

Arpit Nageshwar
⏰ 3 min read

LEGB Rule in Python in Hindi – पूरा Concept Example के साथ

Table of Contents

1. Introduction to LEGB Rule

जब भी हम Python में कोई variable use करते हैं, तो Python के अंदर एक rule काम करता है जो ये decide करता है कि वो variable कहाँ से आएगा। इसी rule को LEGB Rule कहते हैं।

LEGB चार शब्दों का short form है:

  • L – Local
  • E – Enclosing
  • G – Global
  • B – Built-in

असल में जब interpreter को किसी variable का नाम मिलता है, तो वो एक fixed order में search करता है कि वो variable किस जगह defined है। यह search सबसे पहले सबसे नज़दीकी scope यानी Local से शुरू होती है और फिर आगे बढ़ते हुए Built-in scope तक जाती है।

इसे समझने के लिए एक real life example लेते हैं। मान लो आपके घर में कोई चीज़ ढूंढनी है, जैसे pen। सबसे पहले आप अपने बैग में देखोगे (Local), फिर अपने कमरे में (Enclosing), फिर पूरे घर में (Global), और अगर वहाँ भी नहीं मिली तो market जाकर नई खरीदोगे (Built-in)। Python भी बिल्कुल इसी तरीके से variable को ढूंढता है।

यह rule इसलिए important है क्योंकि अगर आपको नहीं पता कि Python किस order में variable ढूंढता है, तो आपको scope से related errors समझ में नहीं आएँगी, जैसे NameError या unexpected value मिलना। बहुत बार students को यह लगता है कि function के अंदर define किया गया variable outside भी दिखेगा, लेकिन LEGB Rule की वजह से ऐसा नहीं होता।

Exam की point of view से भी यह topic बहुत important है क्योंकि scope resolution पर based questions अक्सर पूछे जाते हैं, especially nested functions और closures में। इसलिए इस concept को सिर्फ रटने की बजाय logically समझना ज्यादा फायदेमंद है।

अब आगे हम देखेंगे कि ये चारों scopes actually में होते क्या हैं और ये एक दूसरे से कैसे अलग हैं।

2. LEGB Scopes (Local, Enclosing, Global, Built-in)

अब हम एक-एक करके इन चारों scopes को detail में समझते हैं, ताकि आपको यह clear हो जाए कि कौन सा variable किस scope में आता है।

2.1 Local Scope (L)

Local scope उस variable को कहते हैं जो किसी function के अंदर define किया गया हो। यह variable सिर्फ उसी function के अंदर access किया जा सकता है, function के बाहर उसका कोई अस्तित्व नहीं होता।

def show_marks():
    marks = 90   # local variable
    print("Marks:", marks)

show_marks()
print(marks)   # yaha error aayega

ऊपर के code में marks variable सिर्फ show_marks() function के अंदर ही valid है। जैसे ही function खत्म होता है, वो variable memory से हट जाता है। इसलिए बाहर print करने पर NameError आता है।

2.2 Enclosing Scope (E)

Enclosing scope तब आता है जब एक function के अंदर दूसरा function (nested function) होता है। Inner function, outer function के variables को access कर सकता है, इसे ही Enclosing scope कहते हैं।

def outer():
    city = "Bhopal"

    def inner():
        print("City is:", city)   # enclosing scope se aaya

    inner()

outer()

यहाँ city variable outer() function का local variable है, लेकिन inner() function के लिए यह Enclosing scope बन जाता है क्योंकि inner function, outer function के अंदर defined है।

2.3 Global Scope (G)

Global scope वो variables होते हैं जो पूरे program के top level पर define किए जाते हैं, यानी किसी भी function के बाहर। ये variables पूरे file में कहीं से भी access किए जा सकते हैं।

college = "MANIT"   # global variable

def show_college():
    print("College Name:", college)

show_college()

यहाँ college variable function के बाहर define है, इसलिए यह Global scope में आता है और function के अंदर से भी directly पढ़ा जा सकता है।

2.4 Built-in Scope (B)

Built-in scope Python के वो names होते हैं जो पहले से ही Python language में predefined होते हैं, जैसे print(), len(), int(), range() आदि। इन्हें use करने के लिए हमें कुछ भी import या define करने की जरूरत नहीं होती।

numbers = [1, 2, 3, 4]
print(len(numbers))   # len() built-in scope se aa raha hai

यहाँ len function कहीं भी define नहीं किया गया, फिर भी यह काम कर रहा है क्योंकि यह Python के built-in scope का हिस्सा है, जो सबसे बाहरी scope होता है।

Built-in Scope Global Scope Enclosing Scope Local Scope Search order: Local → Enclosing → Global → Built-in

यह diagram दिखा रहा है कि Python variable को ढूंढते समय सबसे पहले Local scope चेक करता है, फिर Enclosing, फिर Global और आखिर में Built-in scope तक जाता है।

3. LEGB Rule with Example (Step-by-Step) in Python in Hindi

अब तक हमने चारों scopes को अलग-अलग समझा। अब एक ऐसा example लेते हैं जिसमें चारों scopes एक साथ हों, ताकि पूरा LEGB Rule step-by-step clear हो जाए।

x = "Global x"   # Global scope

def outer_function():
    x = "Enclosing x"   # Enclosing scope

    def inner_function():
        x = "Local x"   # Local scope
        print(x)

    inner_function()

outer_function()
print(len(x))   # Built-in scope ka use

अब इसे step-by-step समझते हैं कि यहाँ Python क्या-क्या करता है:

  • Step 1: जब inner_function() call होता है, तो सबसे पहले Python x को Local scope में ढूंढता है। यहाँ x = "Local x" मिल जाता है, इसलिए यही print होगा।
  • Step 2: अगर inner_function के अंदर x define नहीं होता, तो Python Enclosing scope यानी outer_function में जाकर x को ढूंढता, जहाँ उसे "Enclosing x" मिलता।
  • Step 3: अगर Enclosing scope में भी x नहीं मिलता, तो Python Global scope में जाता, जहाँ top level पर x = "Global x" defined है।
  • Step 4: और अगर कहीं भी x नाम का variable नहीं मिलता, तो Python आखिर में Built-in scope check करता है, जहाँ normally ऐसे custom नाम नहीं होते, इसलिए वहाँ NameError आ जाता।

इस program के output में सबसे पहले inner_function() से Local x print होगा, क्योंकि Local scope की priority सबसे ज्यादा होती है। इसके बाद print(len(x)) line में x यहाँ Global scope का "Global x" होगा (क्योंकि यह line function के बाहर है), और len() function Built-in scope से आएगा।

एक और important बात यह है कि अगर आप किसी function के अंदर किसी global variable की value को directly change करना चाहते हैं, तो आपको global keyword use करना पड़ता है, वरना Python एक नया local variable बना देगा।

score = 50   # global variable

def update_score():
    global score
    score = 100   # ab yeh global variable ko hi update karega

update_score()
print(score)   # output: 100

अगर यहाँ global score नहीं लिखा जाता, तो function के अंदर score = 100 एक नया Local variable बना देता और असली Global score 50 ही रहता। इसी तरह Enclosing scope के variable को modify करने के लिए nonlocal keyword use किया जाता है।

inner_function outer_function Global (x) Local variable "x" milte hi search yahi ruk jaati hai Output: "Local x" print hota hai (Local scope ki priority sabse jyada)

यह diagram उस example के flow को दिखा रहा है, जिसमें Python पहले inner_function के Local scope में x ढूंढता है और वहीं मिल जाने से आगे search नहीं करता।

Exam की तैयारी करते समय ध्यान रखें कि LEGB Rule सिर्फ variable को read करते समय apply होता है। अगर आपको function के अंदर से बाहर के variable की value को modify करना है, तो global या nonlocal keyword का use करना जरूरी होता है, वरना Python एक बिल्कुल नया local variable create कर देता है।

Arpit Nageshwar

✍️ Arpit Nageshwar

Post-graduated | Web Developer | +3 yr Experience | IIT Kharagpur Certified