In Python, a namespace is a mapping from names to objects, such as variables, functions, and classes. Each namespace has its scope, which defines the visibility of names within the code. Understanding namespaces and scope is crucial for writing clean and maintainable Python code. In this article, we’ll explore Python’s namespaces and scope rules, along with examples to illustrate these concepts.
Namespaces in Python
A namespace in Python is like a dictionary that maps names (identifiers) to objects. There are several types of namespaces in Python:
- Built-in Namespace: Contains built-in functions and types like
print()andlist. - Global Namespace: Contains names defined at the top level of a module or script.
- Enclosing Namespace: Used in nested functions, it contains names from the enclosing function.
- Local Namespace: Contains names defined within a function.
Namespaces are created automatically when you define a new function or module. You can access the contents of a namespace using the globals() and locals() functions.
Scope in Python
Scope defines the visibility of names within a program. Python has the following scopes:
- Local Scope: Names defined within a function are in the local scope and are accessible only within that function.
- Enclosing Scope: Names in the enclosing function’s scope are accessible in nested functions.
- Global Scope: Names defined at the top level of a module are in the global scope and are accessible throughout the module.
- Built-in Scope: Contains built-in names like
len()andstr(). These names are accessible everywhere in the code.
Python follows the LEGB (Local, Enclosing, Global, Built-in) rule to resolve names. When you use a name in your code, Python searches for it in the local scope first, then in enclosing functions, followed by the global scope, and finally in the built-in scope.
Example of Namespaces and Scope
# Global namespace
global_var = 10
def example_function():
# Local namespace
local_var = 20
print("Local var in local namespace:", local_var)
print("Global var in local namespace:", global_var)
example_function()
print("Global var in global namespace:", global_var)
# Trying to access local_var in global scope will raise an error
# print(local_var)
In this example, global_var is in the global namespace and is accessible inside example_function() because Python searches for it in the local, enclosing, and global scopes. However, local_var is in the local namespace of example_function() and is not accessible outside the function.
Types of Python namespace
A namespace containing all the built-in names is created when we start the Python interpreter and exists as long as the interpreter runs.
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 name that may exist in different modules does not collide.
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. The following diagram may help to clarify this concept.

Example 1: Scope and Namespace in Python
# global_var is in the global namespace
global_var = 10
def outer_function():
# outer_var is in the local namespace
outer_var = 20
def inner_function():
# inner_var is in the nested local namespace
inner_var = 30
print(inner_var)
print(outer_var)
inner_function()
# print the value of the global variable
print(global_var)
# call the outer function and print local and nested local variables
outer_function()

Let’s break down each line of code and explain it in detail:
global_var = 10: This line declares a variableglobal_varand assigns it the value10. It is in the global namespace because it is defined outside any function.def outer_function():: This line defines a function calledouter_function. Functions in Python create a new namespace, so any variables defined inside this function will be in its local namespace.outer_var = 20: Insideouter_function, this line declares a variableouter_varand assigns it the value20. This variable is in the local namespace ofouter_function.def inner_function():: Insideouter_function, this line defines a nested function calledinner_function. Nested functions create a nested local namespace, so any variables defined insideinner_functionwill be in its nested local namespace.inner_var = 30: Insideinner_function, this line declares a variableinner_varand assigns it the value30. This variable is in the nested local namespace ofinner_function.print(inner_var): Insideinner_function, this line prints the value ofinner_var, which is30.print(outer_var): Insideouter_function, this line prints the value ofouter_var, which is20.print(global_var): This line prints the value ofglobal_var, which is10. Sinceglobal_varis in the global namespace, it is accessible from anywhere in the code.outer_function(): This line calls theouter_function, which in turn callsinner_function. As a result,outer_varandinner_varare printed, displaying20and30respectively.
Example 2: Use of global Keyword in Python
# define global variable
global_var = 10
def my_function():
# define local variable
local_var = 20
# modify global variable value
global global_var
global_var = 30
# print global variable value
print(global_var)
# call the function and modify the global variable
my_function()
# print the modified value of the global variable
print(global_var)

Here, when the function is called, the global keyword is used to indicate that global_var is a global variable, and its value is modified to 30.
So, when the code is executed, global_var is printed first with a value of 10, then the function is called and the global variable is modified to 30 from the inside of the function.
And finally the modified value of global_var is printed again.
Conclusion
Understanding namespaces and scope in Python is essential for writing efficient and maintainable code. By understanding how namespaces work and how Python resolves names, you can avoid naming conflicts and write more robust programs. Practice using namespaces and scope in your code to become more proficient in Python programming.





Leave a Reply