Posted By Naveen Singh on 05 May 2023
python-program
जब हम किसी variable को create करते है तो वहां पर उस variable का एक क्षेत्र(region) होता है जिसके अन्दर ही वह variable available होता है। तो इसी क्षेत्र(region) को हम scope कहते है।
scope का मतलब होता है की हमने जिस variable को create किया है उसे हम उस region में ही access कर सकते हैं।
Python में scope basically दो प्रकार के होते हैं-:
local scope वे हैं जो किसी function के अन्दर initialize होते हैं और उस function के लिए अद्वितीय(unique) होते हैं। इसे function के बाहर access नहीं किया जा सकता है। आइए देखें कि local
variable कैसे बनाया जाता है।
Example-:
def function():
a = 10
print(a)
function()
Output-:
10
python code में main body में जो variable create किया जाता है वे global variable होते हैं जो की global scope के साथ संबंधित होते हैं।
Example-:
a = 10
def function():
print(x)
function()
print(a)
Output-:
10
अगर आप किसी variable को globally use करना चाहते है और वो variable locally create हैं तो उस variable को आप global keyword के साथ use कर सकते है।
Example-:
def function():
global a
a = 10
function()
print(a)
Output-:
10
I Hope आपको Python में scope क्या होते है? scope python में कैसे work करता है, और scope के types के बारे में आपको जानकारी मिली होगी। यदि आपको कोई doubt है तो
comment section में comment करके पूछ सकते हैं।