Namespaces in Python
by:
blow post content copied from Real Python
click here to view original post
A Python namespace is a mapping from names to objects. It works like a dictionary where keys are object names and values are the objects themselves. Namespaces organize variables and functions in a dedicated space, allowing you to use multiple instances of the same name without conflict, as long as they’re in different namespaces.
In this tutorial, you’ll explore the different types of namespaces in Python, including the built-in, global, local, and enclosing namespaces. You’ll also learn how they define the scope of names and influence name resolution in Python programs.
By the end of this tutorial, you’ll understand that:
- Python namespaces serve as containers that map names to objects, allowing for organized access and management of variables, functions, classes, and objects in general.
- Namespace and scope differ in that a namespace maps names to objects, while a scope is the region of code where you can access a name.
- Python implements most namespaces using dictionaries, where each namespace’s lifecycle is tied to the execution context, such as global or local scopes.
Understanding how namespaces work will improve your ability to manage and organize code efficiently in Python programs, helping to prevent name conflicts and other issues. To get the most out of this tutorial, you should be familiar with Python variables and functions. Knowledge of inner functions and closures will also be a plus.
Get Your Code: Click here to download the free sample code that you’ll use to learn about namespaces and scope in Python.
Take the Quiz: Test your knowledge with our interactive “Namespaces in Python” quiz. You’ll receive a score upon completion to help you track your learning progress:
Interactive Quiz
Namespaces in PythonIn this quiz, you'll test your understanding of Python namespaces. These concepts are crucial for organizing the symbolic names assigned to objects in a Python program and ensuring they don't interfere with one another.
Getting to Know Namespaces in Python
A namespace is a container that holds the currently defined symbolic names and the objects each name references. You can think of a namespace as a dictionary, in which the keys are object names and the values are the objects themselves. Each key-value pair maps a name to its corresponding object.
Namespaces let you use the same name in different contexts without collisions. It’s like giving everything its own little room in a well-organized house. They allow Python to keep things organized, prevent naming conflicts, support the concept of scope, and enforce modularity.
Namespaces are so crucial in Python that they were immortalized in The Zen of Python:
Namespaces are one honking great idea—let’s do more of those!
— Tim Peters
As Tim Peters suggests, namespaces aren’t just great. They’re honking great, and Python uses them extensively. Depending on how you structure your code, a Python program can have up to four different types of namespaces:
- Built-In
- Global
- Local
- Enclosing or nonlocal
These namespaces have differing lifetimes. As Python executes a program, it creates namespaces as necessary and removes them when it no longer needs them. Typically, many namespaces will exist at any given time.
The Python global, local, and nonlocal namespaces are implemented as dictionaries. In contrast, the built-in namespace isn’t a dictionary but a module called builtins
. This module acts as the container for the built-in namespace.
In the following sections, you’ll learn about these four namespaces and what their content and behavior are.
The Built-in Namespace
The built-in namespace contains the names of all of Python’s built-in objects. This namespace is available while the Python interpreter is running. So, you can access the names that live in this namespace at any time in your code without explicitly importing them.
You can list the objects in the built-in namespace with the dir()
function using the __builtins__
object as an argument:
>>> dir(__builtins__)
[
'ArithmeticError',
'AssertionError',
'AttributeError',
'BaseException',
...
'super',
'tuple',
'type',
'vars',
'zip'
]
You may recognize some objects here, such as built-in exceptions, built-in functions, and built-in data types. Python creates the built-in namespace when it starts and keeps it active until the interpreter terminates.
The Global Namespace
The global namespace contains the names defined at the module level. Python creates a main global namespace when the main program’s body starts. This namespace remains in existence until the interpreter terminates.
Additionally, each module has its own global namespace. The interpreter creates a global namespace for any module that your program loads with the import
statement. For further reading on main functions and modules in Python, see the following resources:
Read the full article at https://realpython.com/python-namespaces-scope/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
April 14, 2025 at 07:30PM
Click here for more details...
=============================
The original post is available in Real Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Post a Comment