Python Variable Scope In Hindi

📔 : Python 🔗

Variable Scope , किसी भी Variable के लिए एक accessible area / portion होता है जहाँ किसी Variable को define करने के बाद उसे access किया जा सके।


Python में Variable Scope दो तरह के होते हैं - Global Variable, Local Variable.

Python Global variables

Global Variables वो variables होते है जो कहीं से भी accessible होते हैं means , define करने के बाद पूरी script file में कही भी access कर सकते हैं या किसी function के के अंदर भी access कर सकते है।


बैसे देखा जाये तो अभी तक आपने जो भी variables define किये थे वो Global variables ही थे जिन्हे आप script में कही भी access कर सकते हैं।

Python Global variable Example

Copy Fullscreen Close Fullscreen Run
a = 390
b = 90
def get_adition() :
	print('Addition is :', a+b)

get_adition()
Output
C:\Users\Rahulkumar\Desktop\python>python global_variables.py
Addition is : 480

example में आप देख सकते हैं कि , function के बाहर define किये गए variables (a , b) को function के अंदर easily access कर सकते हैं।

अभी अगर आप Python Functions के बारे में ज्यादा नहीं जानते तो कोई बात नहीं , आगे function के बारे में deeply पढ़ेंगे।

Python Local Variables

Local Variables बो variables होते हैं जो किसी code of block ( जैसे : function ) के अंदर define किये जाते हैं और उसी function के अंदर ही accessible होते हैं , लेकिन इन variables को उस function के बाहर access नहीं कर पाएंगे।

Python Local variable Example

Copy Fullscreen Close Fullscreen Run
def get_adition() :
        def get_adition() :
	a = 390
	b = 90
	print('Addition is :', a+b)

get_adition()
Output
C:\Users\Rahulkumar\Desktop\python>python local_variables.py
Addition is : 480

Important
और आप उस function के बाहर access करते हो error आ जायगी।
NameError: name 'variable_name' is not defined


Python Difference Between Global and Local variable

अगर आपने same name के variables function के अंदर और बाहर define किये तो वह variable as a local variable ही treat होगा। और function में उस variable को use करने से पहले ही define करना होगा।
See Example -

Copy Fullscreen Close Fullscreen Run
a = 40
def get_adition() :
	a = 90
	print('Value of a inside function is : ', a)

get_adition()
print('Value of a outside function is : ', a)
Output
C:\Users\Rahulkumar\Desktop\python>python variable.py
Value of a inside function is :  90
Value of a outside function is :  40

Note : क्योंकि function के अंदर का variable as a local variable treat होगा इसलिए इसे आप पहले use नहीं कर सकते हैं , या फिर variable का name change करें।

a = 40
def get_adition() :
	print('Value of a inside function is : ', a)
	a = 90

get_adition()
Output
C:\Users\Rahulkumar\Desktop\python>python variable.py
Traceback (most recent call last):
  File "variable.py", line 6, in 
    get_adition()
  File "variable.py", line 3, in get_adition
    print('Value of a inside function is : ', a)
UnboundLocalError: local variable 'a' referenced before assignment

I Hope, अब आप Python में variable scope के बारे में अच्छे से समझ गए होंगे।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers