What is a namespace in Python?

A Namespace in Python is a container that holds a set of identifiers (variable names) and their associated objects (values). It helps implement the concept of scope in your program, determining which variables are accessible at any given point in your code.

Every time a new scope is created?like when a function is defined or executed?a new namespace is created. This namespace acts as an "evaluation context" for the identifiers defined within it.

Types of Namespaces

Following are the three types of namespaces in Python:

  • Local Namespace ? Created for each function, method, or class block. Variables defined inside a function are local to that function.
  • Global Namespace ? Created when a module is imported or a script is executed. It contains names defined at the top level of a module or script.
  • Built-in Namespace ? Contains the names of built-in functions and exceptions (like print(), len(), etc.) and is available throughout the Python program.

Namespace Resolution Order

When you refer to a variable, Python searches for it using the LEGB Rule in the following order:

Local Enclosing Global Built-in Python Variable Resolution (LEGB Rule) Search order: Local ? Enclosing ? Global ? Built-in

Local Namespace Example

Every time a function is called, a local namespace is created for its scope ?

def local_namespace_demo():
    a = 10  # Local variable
    print("Inside function:", a)

local_namespace_demo()

try:
    print(a)  # This will raise NameError
except NameError as e:
    print("Error:", e)
Inside function: 10
Error: name 'a' is not defined

Global Namespace Example

We can declare a global variable using the global keyword to modify its value from within a function ?

x = 10  # Global variable

def modify_global():
    global x
    x = 15  # Modify the global variable
    print("Modified global x inside function:", x)

modify_global()
print("Global x after function call:", x)
Modified global x inside function: 15
Global x after function call: 15

Nonlocal Namespace Example

The nonlocal keyword allows you to reference variables from the nearest enclosing scope (not global) ?

def outer_function():
    y = 20  # Enclosing scope variable

    def inner_function():
        nonlocal y
        y += 5  # Modify the enclosing scope variable
        print("Modified nonlocal y inside inner_function:", y)

    inner_function()
    print("Nonlocal y after inner_function call:", y)

outer_function()
Modified nonlocal y inside inner_function: 25
Nonlocal y after inner_function call: 25

Built-in Namespace Example

Python's built-in namespace contains pre-defined functions and exceptions that are always available ?

# These are from built-in namespace
print("Length of 'Python':", len("Python"))
print("Type of 42:", type(42))
print("Absolute value of -5:", abs(-5))

# You can see all built-in names
import builtins
print("Number of built-in names:", len(dir(builtins)))
Length of 'Python': 6
Type of 42: <class 'int'>
Absolute value of -5: 5
Number of built-in names: 152

Conclusion

Namespaces in Python organize variable names into different scopes using the LEGB rule (Local, Enclosing, Global, Built-in). Understanding namespaces helps you write cleaner code and avoid variable naming conflicts.

Updated on: 2026-03-24T16:58:41+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements