Namespace in Python is a system to uniquely identify each and every object. In Python, everything is an object. An object can be a variable, function, or method. Let’s, for example, consider the same file present in multiple directories in your computer. How do you reach each individual file even though they are the same? Using complete absolute path, right. This is the concept of namespace. One object might be present in multiple places in the code, and using namespace python interpreter can know which object is being referenced. Simply, we can say that a namespace is a dictionary of names of objects mapping to the object.
Types of Namespace
- Built-in Namespace
- Global Namespace
- Enclosed Namespace
- Local Namespace
The lifetime of a Namespace: The lifetime of a namespace depends on the scope of the object. Once the scope of the object ends, the lifetime of the namespace ends. That s why it is not possible to access inner namespace objects from outer namespaces.
Scope of Objects in Python:
Scope refers to the coding region from which a particular object is accessible. It is the scope that creates accessibility restrictions on the object. Thus, an object can be accessed only if it is in the current scope.
Namespace is one of the most important concepts in Python and if not understood properly, it might create problems. So, it’s always advised to invest some time to understand it properly.