Python में Scope of Objects in hindi

Arpit Nageshwar
⏰ 3 min read

Python में Scope of Objects and Names क्या है? – Local और Global Scope पूरी जानकारी हिंदी में

Table of Contents

Introduction to Scope – Scope क्या होता है

जब भी हम Python में कोई variable, function या object बनाते हैं, तो एक सवाल हमेशा आता है — यह variable कहाँ-कहाँ use हो सकता है? इसी सवाल का जवाब देता है Scope

  • Scope का मतलब है वह area या region जहाँ किसी particular name (variable, function, class) को access किया जा सकता है।

  • अगर आसान भाषा में समझें, तो Scope यह decide करता है कि कोई नाम program के किस हिस्से में "दिखता" है और किस हिस्से में नहीं।

  • Python में हर variable एक namespace से जुड़ा होता है, और namespace basically एक dictionary की तरह होता है जिसमें names और उनकी values store होती हैं।

  • Scope यह तय करता है कि किसी namespace में मौजूद name को हम program के किस block, function या module से access कर सकते हैं।

  • अगर एक बार यह समझ आ गया कि Scope कैसे काम करता है, तो बहुत सारी confusing errors जैसे NameError या unexpected variable value वाली problems अपने आप clear हो जाती हैं।

  • Scope सिर्फ एक theory topic नहीं है — यह हर real project में matter करता है, चाहे वो छोटा सा script हो या बड़ा software application।

एक simple example से समझते हैं:

x = 10

def show_value():
    print(x)   # yaha outer x access ho raha hai

show_value()
print(x)

यहाँ x function के बाहर define हुआ है, फिर भी function के अंदर से accessible है। लेकिन यह हमेशा इतना simple नहीं रहता — जैसे ही हम function के अंदर x को modify करने की कोशिश करते हैं, चीजें थोड़ी अलग तरीके से behave करती हैं। यही वजह है कि Scope को detail में समझना जरूरी हो जाता है।

Program (Module Level) Global Namespace x = 10 accessible everywhere Local Namespace show_value() ke andar ke variables

यह diagram दिखा रहा है कि Global Namespace पूरे program में accessible होता है, जबकि Local Namespace सिर्फ उस function तक सीमित रहता है जिसमें वह बना है।

Types of Scope (Local and Global) in Hindi

Python में मुख्य रूप से scope को दो हिस्सों में समझा जाता है — Local Scope और Global Scope। इनके अलावा Enclosing और Built-in scope भी होते हैं, जिनकी बात हम आगे LEGB rule में करेंगे, लेकिन अभी focus सिर्फ Local और Global पर रखते हैं।

Local Scope in Python

  • Local Scope उस area को कहते हैं जो किसी function या block के अंदर बनता है।

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

  • Function खत्म होते ही उसका local scope भी destroy हो जाता है, यानी उसमें बने variables memory से हट जाते हैं।

  • यही वजह है कि अगर हम function के बाहर से local variable को access करने की कोशिश करें, तो Python NameError throw करता है।

def calculate_area(length, width):
    area = length * width   # 'area' local variable hai
    return area

result = calculate_area(5, 3)
print(result)

print(area)   # Error: 'area' yaha defined nahi hai

ऊपर के example में area सिर्फ calculate_area() function के अंदर मौजूद है। जैसे ही function अपना काम पूरा करता है, area variable भी memory से clear हो जाता है। इसे ही हम कहते हैं local scope।

Global Scope in Python

  • Global Scope उस area को कहते हैं जो पूरे module या file के level पर होता है।

  • जो variable किसी function के बाहर, यानी सीधे program के main body में define होता है, वह global variable कहलाता है।

  • Global variable को file के किसी भी हिस्से से पढ़ा (read) जा सकता है — चाहे वो किसी भी function के अंदर क्यों न हो।

  • लेकिन अगर हमें किसी function के अंदर से global variable की value को modify करना है, तो सिर्फ पढ़ने से काम नहीं चलता — इसके लिए global keyword का इस्तेमाल करना पड़ता है।

counter = 0   # global variable

def increase_counter():
    global counter
    counter += 1

increase_counter()
increase_counter()
print(counter)   # Output: 2

अगर हम यहाँ global counter लाइन हटा देते, तो Python यह मान लेता कि हम एक नया local variable बना रहे हैं, और यह error आ जाता कि counter को assign करने से पहले reference किया गया है। यही Local और Global Scope के बीच का असली फर्क है — read करने की आजादी सबको है, लेकिन modify करने के लिए permission clearly बतानी पड़ती है।

Global Scope: counter = 0 Local Scope function ke andar global counter counter += 1 Bina global keyword naya local "counter" ban jaata -> UnboundLocalError

यह diagram दिखा रहा है कि global keyword लगाने पर function असली global variable को modify करता है, जबकि बिना keyword के Python उसे नया local variable समझकर error देता है।

Scope Resolution in Python in Hindi – LEGB Rule

जब Python किसी variable को access करता है, तो वह उसे एक fixed order में ढूँढता है। इसी order को LEGB Rule कहा जाता है। LEGB चार शब्दों का short form है:

  • L – Local: सबसे पहले Python current function के local scope में variable को ढूँढता है।

  • E – Enclosing: अगर local में नहीं मिला, तो वह outer function (अगर कोई nested function है) के scope में देखता है।

  • G – Global: उसके बाद वह module-level यानी global scope में search करता है।

  • B – Built-in: अगर वहां भी नहीं मिला, तो Python अपने built-in names (जैसे len, print, range) में देखता है।

  • अगर इन चारों जगह में से कहीं भी वह name नहीं मिलता, तो Python NameError throw कर देता है।

इसे एक nested function के example से समझते हैं:

message = "Global message"   # Global scope

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

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

    inner_function()
    print(message)

outer_function()
print(message)

ऊपर के code में तीनों message variables अलग-अलग scope में हैं, इसलिए एक-दूसरे को overwrite नहीं करते। जब inner_function() चलता है, तो Python सबसे पहले local scope में message ढूँढता है और वहीं से value ले लेता है — इसलिए Enclosing या Global scope तक जाने की जरूरत ही नहीं पड़ती।

Scope Resolution को control करने के लिए Python दो special keywords भी provide करता है:

  • global keyword: यह बताता है कि function के अंदर जिस variable को modify किया जा रहा है, वह असल में global scope का variable है, न कि कोई नया local variable।

  • nonlocal keyword: यह nested functions में काम आता है — यह बताता है कि variable enclosing function के scope का है, global का नहीं।

def outer_function():
    count = 0

    def inner_function():
        nonlocal count
        count += 1
        print(count)

    inner_function()
    inner_function()

outer_function()

यहाँ nonlocal count की वजह से inner_function() असली outer_function वाले count को ही update करता है, नया local variable नहीं बनाता। अगर nonlocal हटा दिया जाए, तो Python यही मान लेगा कि count एक नया local variable है, और count += 1 line पर error आ जाएगा क्योंकि उसे पहले assign ही नहीं किया गया।

Exam की नजर से देखा जाए तो LEGB Rule को याद रखने का सबसे आसान तरीका यही है — Python हमेशा "सबसे अंदर से सबसे बाहर" की तरफ search करता है, यानी पहले Local, फिर Enclosing, फिर Global, और आखिर में Built-in। यही एक line समझ में आ जाए, तो Scope Resolution से जुड़ा कोई भी सवाल आसानी से solve हो सकता है।

Arpit Nageshwar

✍️ Arpit Nageshwar

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