Python  

How Python’s Namespace Concept Differs from Other Programming Languages

What Is a Namespace?

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 TypeExampleLifetime
Built-in namespacelenprinttypeCreated when Python interpreter starts
Global namespaceVariables defined at module levelCreated when the module is loaded
Local namespaceVariables inside a functionCreated when function is called
Enclosing (nonlocal)In nested functionsExists 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

FeaturePythonC#/Java/C++
DefinitionNamespace = dictionary mapping names to objectsNamespace = logical grouping of classes or symbols
Runtime BehaviorExists at runtime and can be modified dynamicallyMostly compile-time constructs for organizing code
ImplementationBacked by actual dictionary objects (__dict__)Static structure, no runtime modification
AccessEverything is looked up dynamicallyNames resolved at compile time
Scope ModelFollows LEGB rule (Local-Enclosing-Global-Built-in)Follows block or class scope; no dynamic lookup chain
FlexibilityCan create or modify namespaces dynamically using globals()locals(), or vars()Namespaces are static; cannot be altered at runtime
Modules as NamespacesEach module is a namespacePackages/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

ConceptPythonOther Languages
Namespace TypeRuntime object (dict-like)Compile-time construct
Dynamic or StaticDynamicStatic
CreationAutomatic for each module, class, or functionDefined explicitly using namespace or package keywords
LookupDynamic LEGB chainStatic resolution by the compiler
Modifiable at runtimeYesNo

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.