Namespaces and Scope in Python

What is Namespace in Python?
A namespace is a set of currently defined symbolic names that also includes information about the object that each name refers to. A namespace can be compared to a dictionary, with the keys being the object names and the values being the objects themselves. Each key-value pair corresponds to a specific object.
Namespaces aren’t merely useful, as Tim Peters points out. They’re fantastic, and Python makes heavy use of them. There are four sorts of namespaces in Python programme:
- Built-In
- Global
- Enclosing
- Local
Built-In Namespace
These have varying lifespans. Python creates namespaces as needed and deletes them when they’re no longer needed when a programme runs. At any given time, there will most likely be a large number of namespaces.
Some functions, such as input(), print(), and type(), are always there when we execute the Python interpreter without adding any user-defined functions, classes, or modules.
When the Python interpreter starts up, it constructs the built-in namespace. Until the interpreter is terminated, this namespace exists.
Global Namespace
Any names defined at the main programme level are stored in the global namespace. When the main programme body starts, Python generates the global namespace, which exists until the interpreter quits.
In a strict sense, this may not be the only global namespace available. Any module that your programme loads using the import statement is also given a global namespace by the interpreter.
Also Read: Former Microsoft CEO Ballmer does about-face on Linux technology
The Local and Enclosing Namespaces
When a function is executed, the interpreter establishes a new namespace, as you learned in the last tutorial on functions. That namespace is unique to the function and exists until the function is closed.
Each of these namespaces exists until the function it belongs to is finished. When those namespaces’ functions end, Python may not instantly reclaim the memory allocated to them, but any references to the objects they contain become invalid.
Scope of Variables in Python
Because there are many, distinct namespaces, several different instances of the same name can exist at the same time while a Python programme is running. As long as each instance is in its own namespace, it will be maintained independently and will not interfere with the others.
However, suppose you refer to the name x in your code, and x is found in many namespaces. Python has no way of knowing which one you’re referring to.
The concept of the scope holds the key to the solution. The region of a programme in which a name has meaning is referred to as the scope of a name. The interpreter determines this at runtime based on the location of the name definition and where the name is referenced in the code.
Dictionaries for Python Namespaces
When namespaces were first introduced in this course, you were encouraged to think of them as a dictionary with the keys being the object names and the values being the objects themselves. In fact, that’s exactly what they are for global and local namespaces! These namespaces are implemented as dictionaries in Python.
Globals() and locals() are built-in Python functions that allow you to access global and local namespace dictionaries.
The globals() function
globals() is a function that returns a list of global variables.
A reference to the current global namespace dictionary is returned by the built-in function globals(). You can use it to access the objects in the global namespace.
The locals() function
locals() is a function that returns a list of locals.
Python also has a built-in function called locals that does the same thing (). It’s identical to globals(), except it only works with items in the local namespace.
Change variables that aren’t in scope
Argument passing in Python is similar to pass-by-value and pass-by-reference, as you learned earlier in this series in the tutorial on user-defined Python methods. A function can occasionally adjust its argument in the calling environment by changing the relevant parameter, but it can’t always:
● A function can never change an immutable argument.
- A mutable argument can’t be redefined wholesale, but it can be modified in place.
When a function attempts to modify a variable outside of its local scope, a similar issue occurs. A function can’t make any changes to an immutable object outside of its local scope.
The nonlocal Declaration
Nested function definitions are in a similar scenario. A function can access and alter an object in the global scope using the global declaration. What happens if an enclosed function has to change something in the enclosing scope?
Conclusion
An object is anything that a Python programme uses or interacts with. Even basic software can generate a large number of different things. They’ll probably number in the thousands in more complicated software. Python utilises namespaces to keep track of all of these objects and their names.
You learned the following in this tutorial:
- When Python creates a new namespace, what are the distinct namespaces?
- Python’s namespace implementation structure
- In a Python programme, namespaces define scope.
- The fact that each function in Python has its own namespace is used in a variety of programming techniques.