Global vs Local Variables in Python | Global vs Local Variable Explained in Hindi | Python Tutorial
Python में Global vs Local Variables: विस्तृत गाइड
Python में variables को काम करना सीखना एक fundamental concept है। लेकिन क्या आप जानते हैं कि Global और Local variables में क्या अंतर होता है? इस blog post में हम इन दोनों concepts को विस्तार से समझेंगे और practical examples के साथ सीखेंगे।
Introduction: Variable Scope क्या होता है?
Python में हर variable का एक scope होता है। Scope से मतलब है कि variable कहाँ use हो सकता है और कहाँ नहीं। जब आप कोई variable बनाते हैं, तो वह केवल एक specific area में accessible होता है। इसी concept को variable scope कहते हैं।
Python में मुख्य रूप से दो types के scopes होते हैं:
- Global Scope - पूरे program में accessible
- Local Scope - केवल function के अंदर accessible
Local Variables क्या हैं?
Local variables वे variables होते हैं जो किसी function के अंदर define किए जाते हैं। ये variables केवल उसी function के अंदर use हो सकते हैं। जैसे ही function का execution पूरा हो जाता है, वह variable delete हो जाता है।
Local variables की विशेषताएं:
- Function के अंदर define किए जाते हैं
- केवल उस function के अंदर accessible होते हैं
- Function खत्म होने के बाद memory से delete हो जाते हैं
- दूसरे functions में same name का variable हो सकता है
Global Variables क्या हैं?
Global variables वे variables होते हैं जो किसी भी function के बाहर, program के सबसे top level पर define किए जाते हैं। ये variables पूरे program में कहीं भी use हो सकते हैं।
Global variables की विशेषताएं:
- Function के बाहर define किए जाते हैं
- पूरे program में कहीं भी accessible होते हैं
- Program के execution के दौरान memory में रहते हैं
- सभी functions में use किए जा सकते हैं
Global vs Local Variables - अंतर तालिका
| विशेषता | Local Variables | Global Variables |
|---|---|---|
| Definition Location | Function के अंदर | Function के बाहर |
| Scope | केवल function के अंदर | पूरे program में |
| Memory Management | Function खत्म होते ही delete | पूरे program के execution तक memory में रहता है |
| Lifetime | सिर्फ function call के दौरान | Program शुरू होने से खत्म होने तक |
| Access Keyword | — | global keyword से function के अंदर modify कर सकते हैं |
Practical Example: Global vs Local Variables
चलिए एक simple example से समझते हैं कि दोनों variables actually काम कैसे करते हैं:
x = 10 # Global variable
def show_number():
y = 5 # Local variable
print("Local y:", y)
print("Global x:", x) # Global variable ko function ke andar read kar sakte hain
show_number()
print("Outside x:", x)
# print(y) # Ye line Error dega, kyunki y sirf function ke andar accessible hai
global Keyword का इस्तेमाल कैसे करें?
अगर आपको किसी function के अंदर global variable को modify (change) करना है, तो सिर्फ उसे access करना काफी नहीं है — आपको global keyword use करना पड़ेगा:
counter = 0 # Global variable
def increment():
global counter
counter += 1 # Ab ye global variable ko permanently modify karega
increment()
increment()
print(counter) # Output: 2
अगर आप global keyword नहीं लिखते और सीधे counter += 1 कर देते हैं, तो Python एक नया local variable समझ लेगा और UnboundLocalError throw करेगा।
UnboundLocalError क्यों आता है?
ये एक बहुत common mistake है जो beginners करते हैं:
total = 100
def update_total():
total = total + 50 # Error!
print(total)
update_total()
# UnboundLocalError: local variable 'total' referenced before assignment
ऐसा इसलिए होता है क्योंकि Python function के अंदर total को assign होते देखकर उसे local variable मान लेता है, और assignment से पहले उसे read करने की कोशिश करता है — जिससे error आता है। इसे ठीक करने के लिए global total लिखना जरूरी है।
Best Practices: Global Variables कब Avoid करें?
- Global variables को जितना हो सके कम इस्तेमाल करें — ये code को debug करना मुश्किल बना देते हैं
- Function के बीच data pass करने के लिए parameters और return values इस्तेमाल करें
- अगर multiple functions एक ही data पर काम कर रहे हैं, तो class/object based approach बेहतर होता है
- Naming clash से बचने के लिए local variables को meaningful names दें
Related Posts
- Python Function में pass Statement क्यों लगाते हैं?
- Python If Else in Hindi – Conditional Statements समझें
- Python For Loop in Hindi – Complete Guide
- Python While Loop in Hindi – Episode 19
- Python Tutorial in Hindi – Beginner to Advanced (Pillar Page)
FAQs: Global vs Local Variables in Python
1. Global variable को function के अंदर modify कैसे करें?
global keyword इस्तेमाल करें। उदाहरण के लिए: global counter लिखने के बाद आप उस variable को function के अंदर बदल सकते हैं और change पूरे program में reflect होगा।
2. अगर Local और Global variable का नाम same हो तो क्या होगा?
Function के अंदर Python हमेशा local variable को priority देता है। जब तक function खत्म नहीं होता, उस function के अंदर वही नाम local variable को refer करेगा, global variable को नहीं (जब तक global keyword ना लिखा हो)।
3. UnboundLocalError क्यों आता है?
ऐसा तब होता है जब आप किसी variable को function के अंदर assign करने की कोशिश करते हैं, लेकिन Python उसे local मान लेता है और assignment से पहले access करने पर error आता है। Solution: global keyword add करें।
4. क्या Local variables दूसरे functions में इस्तेमाल हो सकते हैं?
नहीं। Local variables सिर्फ उसी function के अंदर accessible होते हैं जिसमें वो define हुए हैं। Function खत्म होते ही वो memory से delete हो जाते हैं।
5. Global variables ज्यादा इस्तेमाल करना सही है या गलत?
Zaroorat se zyada global variables use karna best practice नहीं मानी जाती, क्योंकि इससे code debug करना और maintain करना मुश्किल हो जाता है। Parameters और return values ka use behtar approach hai।
कोई टिप्पणी नहीं:
एक टिप्पणी भेजें