Python Module Namespace

Python Module Namespace

A namespace is a container that provides a named context for identifiers’. Two identifiers with the same name in the same scope will lead to a name clash.

एक नेमस्पेस एक कंटेनर है जो आइडेंटिफायर के लिए एक नामित संदर्भ प्रदान करता है। एक ही दायरे में एक ही नाम वाले दो पहचानकर्ता नेम क्लैश को जन्म देंगे।

Modules are also the largest program structure in Python programs, and one of the first key concepts in the language. As we’ve seen, Python programs are composed of multiple module files linked together by import statements, and each module file is a package of variables that is, a namespace. Just as importantly, each module is a self-contained namespace: one module file cannot see the names defined in another file unless it explicitly imports that other file. Because of this, modules serve to minimize name collisions in your code–because each file is a self-contained namespace, the names in one file cannot clash with those in another, even if they are spelled the same way.

पायथन प्रोग्रामों में मॉड्यूल भी सबसे बड़ी प्रोग्राम संरचना है, और भाषा में पहली प्रमुख अवधारणाओं में से एक है। जैसा कि हमने देखा है, पायथन प्रोग्राम इंपोर्ट स्टेटमेंट द्वारा एक साथ लिंक की गई कई मॉड्यूल फाइलों से बने होते हैं, और प्रत्येक मॉड्यूल फाइल वैरियेबल का एक पैकेज है- यानी, एक नेमस्पेस। बस उतना ही महत्वपूर्ण है, प्रत्येक मॉड्यूल एक सेल्फ-कंटेंड नेमस्पेस है: एक मॉड्यूल फाइल किसी अन्य फाइल में परिभाषित नामों को तब तक नहीं देख सकती है जब तक कि वह स्पष्ट रूप से उस अन्य फाइल को इंपोर्ट नहीं करता है। इस वजह से, मॉड्यूल आपके कोड में नाम क्लैश को कम करने के लिए काम करते हैं – क्योंकि प्रत्येक फाइल एक सेल्फ-कंटेंड नेम स्पेस है, एक फाइल में नाम दूसरे में उन लोगों के साथ नहीं टकरा सकते हैं, भले ही वे उसी तरह से स्पेल्ड हों।
Example:

#module gyancs.py
def book ():
    print ("gyancs school book ()") 
    return 100 + 200
#module gyancs.py 
def book () :
    print ("gyancs school book()") 
    return 100 * 200

test1.py
import gyancs
import gyancs
gyancs.book()
gyancs.book()

Output:
gyancs school book()
gyancs school book()

In the above example, Gyancs School and gyancs_publication are imported into the same program testing.py. Each module has a function book (), which return very different results. When we call the book() from the main module, there will be a name clash as it will be difficult to determine which of these two function should be called. Namespaces provide a means for resolving such problems.

उपरोक्त उदाहरण में gyncs_school और gyancs_school का एक ही प्रोग्राम testing.py में इंपोर्ट किया जाता है। प्रत्येक मॉड्यूल में एक फंक्शन बुक) है, जो बहुत अलग परिणाम देता है। जब हम मुख्य मॉड्यूल से बुक) कहते हैं, तो नाम क्लैश होगा क्योंकि यह निर्धारित करना मुश्किल होगा कि इन दोनों में से कौन सा फंक्शन कहा जाना चाहिए। नेमस्पेस ऐसी समस्याओं के समाधान के लिए एक साधन प्रदान करते हैं।

In Python, each module has its own namespace. This namespace includes the names of all properties defined in the module. Therefore, two instances of book (), each defined in their own module, are distinguished by being fully qualified with the name of the module in which each is defined as, Gyancs School.book () and gyancs_school.book ().

पायथन में, प्रत्येक मॉड्यूल का अपना नेमस्पेस है। इस नेमस्पेस में मॉड्यूल में परिभाषित सभी गुणों के नाम शामिल हैं। इसलिए बुक) के दो उदाहरण, प्रत्येक अपने स्वयं के मॉड्यूल में परिभाषित हैं, मॉड्यूल के नाम के साथ पूरी तरह से योग्य होने के द्वारा प्रतिष्ठित हैं जिसमें प्रत्येक को gyancs_school.book () और gyancs_school. book () के रूप में परिभाषित किया गया है।
test1.py
import gyancs
import gyancs
gyancs school.book ()
gyancs school.book ()

Reloading Modules – पुनः लोडिंग मॉड्यूल

The Python interpreter imports a module only once during a session. This makes things more efficient. Here is an example to show how this works.

पायथन इंटरप्रेटर एक सेशन के दौरान केवल एक बार एक मॉड्यूल का आयात करता है। यह चीजों को और अधिक कुशल बनाता है। यहाँ एक उदाहरण है कि यह कैसे काम करता है।

Example 1 : test1.py
import time
from imp import reload
import example
time.sleep (30)
reload (example)
time. sleep (30)
reload (example)
print (“This is testing”)

Note: In the above program, every time updated version of example module will be available to our program.

नोटः उपरोक्त प्रोग्राम में, उदाहरण मॉड्यूल का हर अपडेट किया गया संस्करण हमारे प्रोग्राम के लिए उपलब्ध होगा।

Example 2: Try below example to better understanding of reloading concept.

उदाहरण 2: पुनः लोड करने की अवधारणा की बेहतर समझ के लिए नीचे दिए गए उदाहरण को करने प्रयास करें।
demo1.py
print (“This is Demo module”)

test1.py
import demo1
import demo1
import demo1
import demo1
import demo1
print(“this is test module”)

Output:
This is Demo module
this is test module

In the above program testing.py module will be loaded only once ever though we are importing multiple times. The problem in this approach is after loading a module if it is updated outside then updated version of example is not available to our program.

उपरोक्त प्रोग्राम में testing.py मॉड्यूल को केवल एक बार लोड किया जाएगा, हालांकि हम कई बार इंपोर्ट कर रहे हैं। इस दृष्टिकोण में समस्या एक मॉड्यूल लोड करने के बाद है यदि इसे बाहर अपडेट किया गया है, तो उदाहरण के अपडेटेड वर्जन हमारे प्रोग्राम के लिए उपलब्ध नहीं है।

We can solve this problem by reloading module explicitly based on our requirement. We can reload by using reload() function imp module.

हम अपनी आवश्यकता के आधार पर मॉड्यूल को पुनः लोड करके इस समस्या को हल कर सकते हैं। हम मॉड्यूल प्रति reload() फंक्शन का उपयोग करके पुनः लोड कर सकते हैं।

test1.py
import demo1
from imp import reload
reload (demo1)
reload (demo1)
reload (demo1)
print(“this is test module”)

In the above program demo.py will be loaded 4 times in that one time by default and 3 times

explicitly.

उपरोक्त प्रोग्राम में demo.py 4 बार लोड होगा जिसमे एक बार तो डिफाल्ट रूप से एवं 3 बार एक्सप्लिसिटली लोड किया गया है।

Output:
This is Demo module
This is Demo module
This is Demo module
This is Demo module
this is test module

Chapter wise Model Paper Link

About Me