- What a namespace is ā understanding name containers in Python
- Types of namespaces ā built-in, global, and local
- Built-in namespace ā Python's predefined names
- Global namespace ā module-level names
- Local namespace ā function-level names
- The LEGB rule ā how Python resolves names
- Namespace vs Scope ā understanding the difference
- Hands-on practice with the interactive editor
What is a Namespace?
A namespace in Python is a container that holds a set of identifiers (names) and their corresponding objects. Think of it like a dictionary where the keys are names and the values are the objects they refer to.
Namespaces help Python organize and manage names so that they don't conflict with each other. For example, you can have a variable named x in one function and another variable named x in a different function ā they don't conflict because they exist in different namespaces.
# Different namespaces allow the same name to be used
x = 10 # Global namespace
def my_function():
x = 20 # Local namespace (different from global x)
print(f"Local x: {x}")
my_function()
print(f"Global x: {x}")
# Output:
# Local x: 20
# Global x: 10
š” Key insight: A namespace is like a filing cabinet where each drawer (namespace) can have a file with the same name (variable) without conflict.
Types of Namespaces
Python has three main types of namespaces:
š§ Built-in
Contains Python's built-in functions and exceptions
š Global
Contains names defined at the module level
š¦ Local
Contains names defined inside a function
1. Built-in Namespace
The built-in namespace contains Python's built-in functions, exceptions, and objects. These are available everywhere in your Python code without any import.
# Built-in namespace examples print() # Built-in function len() # Built-in function int() # Built-in function str() # Built-in function # View built-in names import builtins print(dir(builtins)[:10]) # Shows first 10 built-in names
2. Global Namespace
The global namespace contains names defined at the module level ā outside any function or class. These names are accessible throughout the module.
# Global namespace examples
name = "Python" # Global variable
version = 3.12 # Global variable
def show_global():
print(name) # Accessing global variable
show_global()
print(version)
# Output:
# Python
# 3.12
3. Local Namespace
The local namespace contains names defined inside a function. These names are only accessible within that function.
# Local namespace examples
def calculate():
a = 10 # Local variable
b = 20 # Local variable
return a + b
result = calculate()
print(result) # 30
# a and b are not accessible outside the function
# print(a) # NameError: name 'a' is not defined
4. The LEGB Rule
Python uses the LEGB rule to resolve names when they are referenced. LEGB stands for:
L - Local
Names defined inside a function
E - Enclosing
Names in outer functions
G - Global
Names at the module level
B - Built-in
Python's built-in names
# LEGB Rule Example
x = "global" # Global
def outer():
x = "enclosing" # Enclosing
def inner():
x = "local" # Local
print(f"Local x: {x}")
print(f"Built-in: {len('hello')}")
inner()
outer()
# Output:
# Local x: local
# Built-in: 5
5. Namespace vs Scope
While namespaces and scopes are related, they are different concepts:
# Namespace vs Scope Example
# Namespace: The container of names
# Scope: The region where a name is accessible
global_var = "global"
def my_function():
local_var = "local" # Local namespace
print(global_var) # Can access global scope
print(local_var) # Can access local scope
my_function()
# print(local_var) # Error - local_var is not in this scope
Try It Yourself!
Experiment with namespaces directly in your browser. Modify the code and see the results in real time.
NAMESPACE DEMONSTRATION
========================================
1. LOCAL VARIABLE:
I am local
2. ENCLOSING VARIABLE:
I am enclosing
3. GLOBAL VARIABLE:
I am global
4. BUILT-IN FUNCTION:
Built-in len: 5
========================================
NAMESPACE DIFFERENCES
========================================
Global x: 10
Local x: 20
Local x in function2: 30
ā Different namespaces allow same names without conflict!
š You've Mastered Python Namespace!
You understand built-in, global, and local namespaces, the LEGB rule, and the difference between namespace and scope. These concepts are fundamental to Python programming.
Quick Quiz ā Test Your Knowledge
Let's see what you've learned about Python namespaces:
print()?Frequently Asked Questions
š¤ What's the difference between namespace and scope? ā¼
š§ How does Python resolve variable names? ā¼
š Can I modify a global variable inside a function? ā¼
global keyword inside a function to modify a global variable. Without it, Python creates a new local variable with the same name.
š What is an enclosing namespace? ā¼
š Where to Go From Here
Now that you understand Python namespaces, here are some related topics to explore:
š Global & Local Variables
Learn about variable scope
š Functions
Learn about functions in Python
š¦ Modules
Learn about Python modules