Use namespace in python

Python Namespaces and Scope

In Python, the namespace is used to provide a unique name to each object. Python dictionary is used to maintain the namespace. Dictionaries play an important role in declaring and representing namespace. Here, the name defines the unique name and space defines the scope or location. In Python, namespaces are used to organize objects in the application program. There are three types of namespace exists in python.

Three types of namespace:

  1. Built-In Namespace: All the built-in objects are included in this namespace. Python interpreter plays an important role to create built-in namespace at the time of loading. All these built-in objects that are created at the starting still exist until the termination of the interpreter happened. The given syntax is used to show all the built-in namespace in python. Syntax: dir(__builtins__)
  2. Global Namespace: This namespace is defined at the time of the main program. When the control of the main method body executed, global namespace is created. This namespace exists until the termination of the interpreter happened.
  3. Enclosing Namespace: This namespace is created at the time of execution of the outer function. In the function, the namespace is represented as an enclosed namespace.
  4. Local Namespace: This namespace is created at the time of execution of the function. In the function, the namespace is represented as local. This namespace exists until the termination of the respective function happened.
Читайте также:  Java load library exception

Sample code:

var initial = 0 # the namespace created for this variable is global namespace def outer(): # the namespace is created for this function is enclosing namespace print("This is an outer function") def inner() # the namespace is created for this function is a local namespace print("This is an inner function") return inner() return outer()

Scope of the namespace:

Scope or location defines the memory region where the specific programming application is created. The scope of an individual application program is separated using an internal program and one application cannot interrupt another. The order of searching or scope of the namespace is:

  1. The interpreter first search for the innermost variable or function. So the local namespace gets the first priority.
  2. After local, Enclosing namespace gets the second priority in this searching hierarchy.
  3. Global namespace is getting the thirds priority. The interpreter searches first for local and then enclosing and then search for the global namespace.
  4. Built-In namespaces are executed by the interpreter at the last of this hierarchy.

This is usually known as LEGB rule. Here, L denotes Local, E denotes Enclosing, G donates Global, and B denotes Built-in respectively.

The lifetime of namespaces:

The scope of variables plays an important role to set the lifetime of the namespace. The lifetime of a namespace exists until the scope of the variable comes to an end. An outer namespace cannot access the object of the inner namespace. Multiple namespaces have the same object name but it does not create any ambiguity. As the object names are separated by their respective namespaces.

Читайте также:  Css эффект матового стекла

Dictionaries of the namespace:

Dictionaries are used to implement namespaces in Python. There are two in-built functions available to access namespace dictionaries. One is globals() function and another one is locals() function. But built-in namespace does not use a dictionary, it is implemented by using the Python module.

  1. The globals() function: This function is used to return the instance or reference of currently executing a global namespace dictionary. In global namespace, this instance is used to access the object. It differs in looking base on the version of the operating system and Python version that is installed in the system.
  2. The locals() function: This function is used to provide different types of built-in functions. These functions are called only in the local namespace. This locals() function provides the namespace dictionary in the local namespace. The function parameters are also used in the local namespace. Sometimes, locals() function behaves like a globals() function when locals() are declared outside of a function but inside the main program.

Источник

Namespaces and Scope in Python

A namespace is a system that has a unique name for each and every object in Python. An object might be a variable or a method. Python itself maintains a namespace in the form of a Python dictionary. Let’s go through an example, a directory-file system structure in computers. Needless to say, that one can have multiple directories having a file with the same name inside every directory. But one can get directed to the file, one wishes, just by specifying the absolute path to the file.
Real-time example, the role of a namespace is like a surname. One might not find a single “Alice” in the class there might be multiple “Alice” but when you particularly ask for “Alice Lee” or “Alice Clark” (with a surname), there will be only one (time being don’t think of both first name and surname are same for multiple students).
On similar lines, the Python interpreter understands what exact method or variable one is trying to point to in the code, depending upon the namespace. So, the division of the word itself gives a little more information. Its Name (which means name, a unique identifier) + Space(which talks something related to scope). Here, a name might be of any Python method or variable and space depends upon the location from where is trying to access a variable or a method.

Читайте также:  Input type date css стилизация

Types of namespaces :

When Python interpreter runs solely without any user-defined modules, methods, classes, etc. Some functions like print(), id() are always present, these are built-in namespaces. When a user creates a module, a global namespace gets created, later the creation of local functions creates the local namespace. The built-in namespace encompasses the global namespace and the global namespace encompasses the local namespace.

The lifetime of a namespace :

A lifetime of a namespace depends upon the scope of objects, if the scope of an object ends, the lifetime of that namespace comes to an end. Hence, it is not possible to access the inner namespace’s objects from an outer namespace.

Источник

Оцените статью