Every variable, function and class is associated with a name. As programs grows, Python needs a systematic way to manage these names and determine where they can be accessed. This is achieved through namespaces and scope, which help organize names within different parts of a program.
Namespace
A namespace is a collection of names and their associated objects. Python stores the names of variables, functions and classes in namespaces. This helps organize names and prevents conflicts when the same name is used in different parts of a program. Internally, namespaces are implemented using dictionaries.
name = "Emma"
def greet():
message = "Hello"
greet()
Explanation:
- name belongs to the global namespace because it is created outside any function.
- message belongs to the local namespace of the greet() function.
- Even though both variables exist in the same program, Python can identify them correctly because they are stored in different namespaces.
Types of Namespaces
Python mainly uses three types of namespaces. Each namespace is created for a specific purpose and stores names in different parts of a program.

1. Built-in Namespace: contains names that are available automatically in every Python program. These names are provided by Python itself, so you can use them without defining them.
print("Hello")
print(len("Python"))
Output
Hello 6
Explanation:
- print() is a built-in function used to display output.
- len() is a built-in function used to find the length of an object.
- Since these functions belong to the built-in namespace, they can be used directly in any Python program.
2. Global Namespace: contains names that are created at the top level of a program, outside all functions and classes. These names can usually be accessed from anywhere within the same module.
score = 95
def show_score():
print(score)
show_score()
Output
95
Explanation:
- score is created outside the function, so it belongs to the global namespace.
- function show_score() can access score because global names are visible inside functions unless a local variable with the same name exists.
3. Local Namespace: created whenever a function is called. Variables defined inside the function are stored in this namespace. These variables are available only while the function is executing.
def show_age():
age = 25
print(age)
show_age()
Output
25
Explanation:
- age belongs to the local namespace of show_age().
- It can only be accessed inside that function.
- Once the function finishes execution, the local namespace is removed.
Lifetime of a Namespace
The lifetime of a namespace refers to how long that namespace exists in memory. Different namespaces have different lifetimes:
- The built-in namespace exists as long as the Python interpreter is running.
- The global namespace exists until the program finishes execution.
- A local namespace exists only while its function is running.
x = 5
def outer():
y = 10
def inner():
z = 15
inner()
outer()
Explanation:
- x belongs to the global namespace.
- y belongs to the local namespace of outer().
- z belongs to the local namespace of inner().
- After outer() and inner() finish execution, their local namespaces are destroyed.
Accessing Global Variables Inside a Function
A function can directly read a global variable. However, if you want to modify the global variable inside a function, you must use the global keyword.
count = 5
def update():
global count
count += 1
print(count)
update()
Output
6
Explanation:
- count is a global variable.
- The global keyword tells Python to use the global count variable.
- The value is increased from 5 to 6.
Scope of Objects
A scope is the region of a program where a name can be accessed. Even if a variable exists in a program, it cannot always be accessed everywhere. Its accessibility depends on the scope in which it was created.
def outer():
def inner():
num = 10
print("Inside inner:", num)
inner()
print(num)
outer()
Output
ERROR!
Inside inner: 10
Traceback (most recent call last):
File "<main.py>", line 9, in <module>
File "<main.py>", line 7, in outer
NameError: name 'num' is not defined. Did you mean: 'sum'?
Explanation:
- num is created inside the inner() function.
- Its scope is limited to the inner() function.
- When outer() tries to access num, Python cannot find it and raises a NameError.
LEGB Rule
When Python encounters a variable name, it searches for that name in a specific order known as the LEGB Rule. LEGB stands for:
- Local Scope
- Enclosing Scope
- Global Scope
- Built-in Scope
Python searches these scopes one by one and uses the first matching name it finds.
x = "Global"
def outer():
x = "Enclosing"
def inner():
x = "Local"
print(x)
inner()
outer()
Output
Local
Explanation:
- Python first searches for x in the local scope of inner().
- It finds x = "Local" and stops searching.
- Since the name is found in the local scope, the enclosing, global and built-in scopes are not checked.