What is Namespace in Python

What is Namespace in Python

In Python, A namespace is a container where names are mapped to objects; they are used to avoid confusions in cases where same names exist in different namespaces. They are created by modules, functions, classes etc.

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

In Python, you can imagine a namespace as a mapping of every name; you have defined, to corresponding objects. Different namespaces can co-exist at a given time but are completely isolated.

पायथन में, आप हर नाम के मैपिंग के रूप में एक नेमस्पेस की कल्पना कर सकते हैं, आपने संबंधित ऑब्जेक्ट को परिभाषित किया है। विभिन्न नेमस्पेस एक निश्चित समय पर सह-अस्तित्व में हो सकते हैं लेकिन पूरी तरह से अलग-थलग हैं।

A namespace containing all the built-in names is created when we start the Python interpreter and exists as long we don’t exit. This is the reason that built-in functions like id(), print() etc., are always available to us from any part of the program. Each module creates its own global namespace.

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

These different namespaces are isolated. Hence, the same names that may exist in different modules do not crash.

ये विभिन्न नेमस्पेस अलग-थलग हैं। इसलिए, समान नाम जो विभिन्न मॉड्यूल में मौजूद हो सकते हैं क्रैश नहीं होते हैं।

Modules can have various functions and classes. A local namespace is created when a function is called, which has all the names defined in it. Similar, is the case with class or module.

मॉड्यूल में विभिन्न फंक्शन और क्लासेज हो सकती हैं। किसी फंक्शन को कॉल करने पर एक स्थानीय नेमस्पेस बनाया जाता है, जिसमें सभी नाम परिभाषित होते हैं। इसी तरह, क्लास या मॉड्यूल के साथ केस है।

Scope: A scope defines the hierarchical order in which the namespaces have to be searched in order to obtain the mappings of name-to-object (variables). It is a context in which variables exist and from which they are referenced. It defines the accessibility and the lifetime of a variable.

स्कोपः एक स्कोप पदानुक्रमित क्रम को परिभाषित करता है जिसमें नाम-से-ऑब्जेक्ट (वैरियेबल) की मैपिंग प्राप्त करने के लिए नेमस्पेस की खोज करनी होती है। यह एक संदर्भ है जिसमें वैरियेबल मौजूद हैं और जिसमें से उन्हें संदर्भित किया जाता है। यह वैरियेबल की पहुंच और जीवनकाल को परिभाषित करता है।
Example:

pi = 'outer PIvalue' 
def piFun () :
    pi = 'inner PI value' 
    print (pi)
piFun () 
print (pi)

Output:
innerPI value
outer PIvalue

Above program give different outputs because the same variable name ‘pi’ resides in different namespaces, one inside the function piFun() and the other in the upper level. When piFun() gets executed, ‘inner PI value’ is printed as that is ‘pi’ value inside the function namespace. The value ‘outer Pl value’ is printed when ‘pi’ is referenced in the outer namespace. From the above example, we can guess that there definitely is a rule which is followed, in order in decide from which namespace a variable has to be picked.

उपरोक्त कार्यक्रम अलग-अलग आउटपुट देते हैं क्योंकि एक ही वैरियेबल नाम ‘pi विभिन्न नेमस्पेसों में रहता है, एक फंक्शन piFun() और दूसरा ऊपरी स्तर में। जब piFun() निष्पादित हो जाता है, तो ‘inner PI value’ प्रिंट हो जाता है क्योंकि फंक्शन नेमस्पेस के अंदर ‘pi’ वैल्यू होता है। जब बाहरी नेमस्पेस में ‘pi’ को संदर्भित किया जाता है तो बाहरी PI वैल्यू मुद्रित होता है। उपरोक्त उदाहरण से, हम अनुमान लगा सकते हैं कि निश्चित रूप से एक नियम है जिसका पालन किया जाता है, जिसमें यह तय करने के लिए कि किस नाम स्थान से एक वैरियेबल चुनना है।

The Python’s LEGB-rule comes into play, which we will discuss in the next section.

यहीं पर पायथन का LEGB- रूल चलन में है, जिसकी चर्चा हम अगले भाग में करेंगे।

LEGB Rule

We have seen that multiple namespaces can exist independently from each other and that they can contain the same variable names on different hierarchy levels.

हमने देखा है कि कई नेमस्पेस एक-दूसरे से स्वतंत्र रूप से मौजूद हो सकते हैं और वे विभिन्न पदानुक्रम स्तरों पर समान वैरियेबल नाम रख सकते हैं।

The “scope” defines on which hierarchy level Python searches for a particular “variable name” for its associated object.

“स्कोप’ परिभाषित करता है कि किस पदानुक्रम स्तर पर पायथन अपने संबंधित ऑब्जेक्ट के लिए एक विशेष “वैरियेबल नेम’ को सर्च करता है।

Now, the next question is: “In which order does Python search the different levels of namespaces before it finds the name-to-object’ mapping?”

अब, अगला सवाल यह है: “किस क्रम में पायथन ने नेम-टू-ऑब्जेक्ट मैपिंग खोजने से पहले विभिन्न स्तरों के नेमस्पेस खोजे?”

To answer is: It uses the LEGB-rule, which stands for Local -> Enclosed -> Global -> Built-in.

उत्तर देना है: यह LEGB–नियम का उपयोग करता है, जो लोकल -> इनक्लोज्ड -> ग्लोबल -> बिल्ट-इन के लिए हैं।

Where the arrows should denote the direction of the namespace-hierarchy search order.

जहां एरो को नेमस्पेस-पदानुक्रम सर्च आर्डर की दिशा को निरूपित करना चाहिए।

  • Local can be inside a function or class method. It contains names defined inside the current function. लोकल एक फंक्शन या क्लास मेथड के अंदर हो सकता है। इसमें वर्तमान फंक्शन के अंदर परिभाषित नाम शामिल हैं

  • Enclosed can be its enclosing function, e.g., if a function is wrapped inside another function.

इनक्लोज्ड किया जा सकता है इसका इनक्लोजिंग फंक्शन, उदाहरण, यदि कोई फंक्शन किसी अन्य फंक्शन के अंदर त्रैप किया जाता है।

  • Global refers to the uppermost level of the executing script itself, in other word it contains. Names defined at the top level of the script or module, and

ग्लोबल निष्पादन की स्क्रिप्ट के ऊपर के स्तर को संदर्भित करता है, दूसरे शब्द में इसमें स्क्रिप्ट या मॉड्यूल के शीर्ष स्तर पर परिभाषित नाम शामिल हैं, और

  • Built-in are special names that Python reserves for itself.

बिल्ट-इन विशेष नाम हैं जो पायथन अपने लिए आरक्षित रखते हैं।

So, if a particular name: object mapping cannot be found in the local namespaces, the namespaces of the enclosed scope are being searched next. If the search in the enclosed scope is unsuccessful, too, Python moves on to the global namespace, and eventually, it will search the built-in namespace.

इसलिए, यदि किसी विशेष नाम: ऑब्जेक्ट मैपिंग को स्थानीय नेमस्पेसों में नहीं पाया जा सकता है, तो संलग्न क्षेत्र के नाम स्थान अगले खोजे जा सकते हैं। यदि संलग्न क्षेत्र में खोज असफल है, तो भी, पायथन वैश्विक नेमस्पेस पर आगे बढ़ता है, और अंततः, यह अंतर्निहित नेमस्पेस की खोज करेगा।

Note: If a name cannot found in any of the namespaces, python raises an Error Message like … NameError.

नोट: यदि किसी नेमस्पेस में कोई नाम नहीं मिल रहा है, तो पायथन एक एरर मैसेज रेज करता है, जैसे नेमएरर ।

Chapter wise Model Paper Link

About Me