Module Files as Namespaces in Python in Hindi – पूरी जानकारी Examples के साथ
Table of Contents
1. Concept of Namespace in Python in Hindi – नेमस्पेस की अवधारणा
2. Module Namespace in Python in Hindi – मॉड्यूल नेमस्पेस क्या है
3. Accessing Module Members in Python in Hindi – मॉड्यूल के मेंबर्स को Access करना
4. Benefits of Namespaces in Python in Hindi – पायथन में नेमस्पेस के फायदे
जब भी हम Python में कोई बड़ा project बनाते हैं, तो code को अलग-अलग files यानी modules में बाँट देते हैं। लेकिन सवाल यह आता है कि जब दो अलग-अलग files में same नाम का variable या function हो, तो Python confuse क्यों नहीं होता? इसका जवाब है — Namespace। हर module file अपने आप में एक independent namespace की तरह काम करती है, जिससे names आपस में clash नहीं करते। इस blog में हम इसी concept को step by step, simple भाषा में समझेंगे, ताकि exam में भी काम आए और real coding में भी।
Concept of Namespace in Python in Hindi – नेमस्पेस की अवधारणा
Namespace का सीधा मतलब है — एक ऐसी जगह जहाँ names (यानी variables, functions, classes के नाम) store होते हैं और उन्हें unique तरीके से पहचाना जाता है। Python में हर नाम किसी न किसी namespace से जुड़ा होता है, और यही वजह है कि एक ही नाम अलग-अलग जगहों पर अलग-अलग चीज़ों को represent कर सकता है, बिना किसी conflict के।
सोचिए आपके पास दो दोस्त हैं, दोनों का नाम "Raj" है। अगर आप कहें "Raj को बुलाओ" तो confusion हो सकती है। लेकिन अगर आप कहें "Class A वाले Raj को बुलाओ" और "Class B वाले Raj को बुलाओ", तो कोई confusion नहीं रहेगी। Namespace भी बिल्कुल यही काम करता है — यह हर नाम को एक "class" यानी context दे देता है, ताकि Python को पता रहे कि किस नाम की बात हो रही है।
Technically कहें तो, namespace एक mapping होता है जिसमें names को keys की तरह और उनके actual objects (values, functions, classes) को values की तरह store किया जाता है। अंदर से Python इसे एक dictionary जैसे structure में ही maintain करता है।
Python में मुख्य रूप से चार तरह के namespaces होते हैं:
Built-in Namespace:
इसमें Python की built-in functions जैसेprint(),len(),range()आदि आते हैं। यह namespace Python start होते ही बन जाता है और पूरे program के लिए available रहता है।Global Namespace:
यह किसी एक module (file) के top-level पर define किए गए names को रखता है। हर module की अपनी अलग global namespace होती है।Local Namespace:
जब कोई function call होता है, तो उसके अंदर define किए गए variables एक temporary local namespace में store होते हैं, जो function खत्म होते ही खत्म हो जाता है।Enclosing Namespace:
यह तब आता है जब कोई function किसी दूसरे function के अंदर defined हो (nested function)। बाहर वाले function का namespace, अंदर वाले के लिए enclosing namespace बन जाता है।
यही चार namespaces मिलकर Python की famous LEGB Rule (Local → Enclosing → Global → Built-in) बनाते हैं, जिसके through Python यह decide करता है कि किसी नाम को कहाँ ढूंढना है।
Module Namespace in Python in Hindi – मॉड्यूल नेमस्पेस क्या है
जब भी आप कोई .py file बनाते हैं, तो Python उस file को एक module मानता है, और उस module के अंदर define किए गए सारे variables, functions और classes उसी module की global namespace में रहते हैं। इसी को हम Module Namespace कहते हैं।
यानी simple भाषा में — हर module file अपने आप में एक independent "box" होती है, जिसके अंदर उसके names सुरक्षित रहते हैं। जब तक आप उस module को import नहीं करते, तब तक उसके names दूसरी files से directly दिखाई नहीं देते।
चलिए एक छोटा सा example देखते हैं। मान लीजिए हमने एक file बनाई my_module.py:
# my_module.py
course_name = "Python Programming"
total_students = 50
def greet_student(name):
print(f"Namaste {name}, {course_name} course mein aapka swagat hai!")
class Student:
def __init__(self, name):
self.name = name
यहाँ course_name, total_students, greet_student और Student — ये सारे नाम my_module की अपनी namespace के अंदर store हुए हैं। अगर किसी दूसरी file में same नाम का variable, जैसे course_name = "Java Programming", बना दिया जाए, तो भी कोई clash नहीं होगा — क्योंकि दोनों अलग-अलग modules की अलग-अलग namespaces में हैं।
Python के पीछे यह पूरा mechanism __dict__ attribute के through चलता है। हर module, function और class के पास अपना एक __dict__ होता है, जो असल में उसकी namespace को represent करता है। नीचे दिया गया diagram इसी structure को visually समझाता है।
यह SVG दिखाता है कि कैसे my_module.py के सारे names (variables, function, class) एक ही namespace "box" के अंदर organised रहते हैं।
Important बात यह है कि module की namespace तब तक create नहीं होती जब तक module को actually import या run न किया जाए। जैसे ही आप import my_module लिखते हैं, Python उस file को एक बार execute करता है और उसकी पूरी namespace को memory में तैयार कर देता है।
Accessing Module Members in Python in Hindi – मॉड्यूल के मेंबर्स को Access करना
अब जब हमें पता चल गया कि हर module की अपनी namespace होती है, तो सवाल यह आता है कि दूसरी file से उस namespace के अंदर मौजूद variables, functions और classes को कैसे access करें। इसके लिए Python में मुख्य रूप से तीन तरीके इस्तेमाल किए जाते हैं।
1. Dot Notation (module.member)
यह सबसे common और सबसे safe तरीका है। पहले पूरे module को import करते हैं, फिर dot (.) लगाकर उसके अंदर के members को access करते हैं।
# main.py
import my_module
print(my_module.course_name) # Output: Python Programming
print(my_module.total_students) # Output: 50
my_module.greet_student("Aarav") # Output: Namaste Aarav, ...
s1 = my_module.Student("Priya")
print(s1.name) # Output: Priya
यहाँ my_module.course_name लिखकर हम Python को बता रहे हैं कि — "मुझे course_name नाम वाला वो variable चाहिए जो my_module की namespace में है", न कि current file की namespace में।
2. from...import Statement
अगर आपको module का सिर्फ कोई एक specific member चाहिए, तो poore module को import करने की जरूरत नहीं। इस case में from keyword काम आता है:
# main.py
from my_module import greet_student, course_name
greet_student("Riya")
print(course_name)
ध्यान दें — यहाँ greet_student और course_name अब current file की own namespace का हिस्सा बन गए हैं, इसलिए इन्हें बिना dot notation के directly इस्तेमाल किया जा सकता है। लेकिन इसका एक risk भी है — अगर current file में पहले से course_name नाम का कोई variable है, तो वह overwrite हो जाएगा।
3. dir() और __dict__ से Members देखना
अगर आपको यह check करना हो कि किसी module की namespace में कौन-कौन से names मौजूद हैं, तो dir() function बहुत काम आता है:
import my_module
print(dir(my_module))
# ['Student', '__builtins__', '__doc__', '__file__', ...,
# 'course_name', 'greet_student', 'total_students']
इसी तरह my_module.__dict__ लिखकर हम module की पूरी namespace को dictionary के रूप में भी देख सकते हैं, जिसमें हर name उसकी value के साथ key-value pair की तरह दिखता है।
यह SVG dot notation और from...import के बीच का फर्क दिखाता है — पहले तरीके में हम module की namespace से हर बार होकर गुजरते हैं, जबकि दूसरे तरीके में member की एक copy current namespace में आ जाती है।
Exam Tip: अगर पूछा जाए कि "namespace pollution" क्या है, तो याद रखें — from module import * इस्तेमाल करने से module के सारे names current namespace में आ जाते हैं, जिससे naming conflicts की संभावना बढ़ जाती है। इसलिए dot notation को हमेशा ज्यादा safe माना जाता है।
Benefits of Namespaces in Python in Hindi – पायथन में नेमस्पेस के फायदे
1. Name Conflicts से बचाव:
अलग-अलग modules में same नाम के variables या functions होने पर भी कोई clash नहीं होता, क्योंकि हर एक की अपनी अलग namespace होती है।2. Code Organization बेहतर होता है:
बड़े projects में code को logical तरीके से अलग-अलग modules में बाँटा जा सकता है, जिससे code readable और maintainable बनता है।3. Reusability बढ़ती है:
एक module को अलग-अलग projects में बार-बार import करके इस्तेमाल किया जा सकता है, बिना किसी naming issue के।4. Debugging आसान होती है:
चूँकि हर name किसी specific namespace से जुड़ा होता है, इसलिए error आने पर यह पता लगाना आसान हो जाता है कि problem किस module या scope में है।5. Memory Management efficient रहता है:
Local namespaces function खत्म होते ही memory से हट जाती हैं, जिससे unnecessary memory usage कम होता है।6. Encapsulation को support करता है:
Namespace की वजह से module के अंदर की internal details बाहर से directly interfere नहीं कर पातीं, जब तक explicitly import न किया जाए।