What Is a Namespace?
A namespace is simply a mapping between names (identifiers) and objects (values). You can think of it as a container that keeps track of all the names you’ve defined (variables, functions, classes, modules, etc.) and their corresponding objects in memory.
How Python Handles Namespaces
In Python, namespaces are dynamic dictionaries created and managed automatically by the interpreter. Every time you create a variable or function, Python stores it in some namespace.
There are several kinds of namespaces:
| Namespace Type | Example | Lifetime |
|---|
| Built-in namespace | len, print, type | Created when Python interpreter starts |
| Global namespace | Variables defined at module level | Created when the module is loaded |
| Local namespace | Variables inside a function | Created when function is called |
| Enclosing (nonlocal) | In nested functions | Exists as long as the outer function is alive |
These are searched in order: Local → Enclosing → Global → Built-in. This is known as the LEGB rule.
Example
x = 10 # Global namespace
def outer():
y = 20 # Enclosing namespace
def inner():
z = 30 # Local namespace
print(x, y, z)
inner()
outer()
Here, the same variable name could exist in multiple scopes — Python decides which one to use via LEGB lookup.
How This Differs from Other Languages
| Feature | Python | C#/Java/C++ |
|---|
| Definition | Namespace = dictionary mapping names to objects | Namespace = logical grouping of classes or symbols |
| Runtime Behavior | Exists at runtime and can be modified dynamically | Mostly compile-time constructs for organizing code |
| Implementation | Backed by actual dictionary objects (__dict__) | Static structure, no runtime modification |
| Access | Everything is looked up dynamically | Names resolved at compile time |
| Scope Model | Follows LEGB rule (Local-Enclosing-Global-Built-in) | Follows block or class scope; no dynamic lookup chain |
| Flexibility | Can create or modify namespaces dynamically using globals(), locals(), or vars() | Namespaces are static; cannot be altered at runtime |
| Modules as Namespaces | Each module is a namespace | Packages/namespaces are part of compile-time structure |
Example Comparison
Python
# module1.py
x = 5
# module2.pyimport module1
print(module1.x) # Access via namespace object
Here, module1 is an actual object (a namespace in memory).
C#
namespace ProjectA {
class MyClass { public static int x = 5; }
}
class Program {
static void Main() {
Console.WriteLine(ProjectA.MyClass.x);
}
}
Here, namespace ProjectA is just a compile-time container, not an object in memory.
In Short
| Concept | Python | Other Languages |
|---|
| Namespace Type | Runtime object (dict-like) | Compile-time construct |
| Dynamic or Static | Dynamic | Static |
| Creation | Automatic for each module, class, or function | Defined explicitly using namespace or package keywords |
| Lookup | Dynamic LEGB chain | Static resolution by the compiler |
| Modifiable at runtime | Yes | No |
Summary
In Python, namespaces are living, mutable runtime objects that store the mapping of names to objects and can change during execution.
In other languages like C#, Java, or C++, namespaces are compile-time constructs used only to organize code and prevent naming collisions — they do not exist at runtime.