What is Namespace in Python?
A namespace is a collection of currently defined symbolic names along with information about the object that each name references. You can think of a namespace as a dictionary in which the keys are the object names and the values are the objects themselves. Each key-value pair maps a name to its corresponding object.
Types of Namespace

Scope in Namespace in Python

Use of Types of Namespaces?

Example Code of Namespace Types



# var1 is in the global namespace | |
var1 = 5 | |
def some_func(): | |
# var2 is in the local namespace | |
var2 = 6 | |
def some_inner_func(): | |
# var3 is in the nested local | |
# namespace | |
var3 = 7 | |
# As shown in the following figure, the same object name can be present in multiple namespaces as isolation between the same name is maintained by their namespace. | |
# But in some cases, one might be interested in updating or processing global variables only, as shown in the following example, one should mark it explicitly as global and the update or process. Note that the line “count = count +1” references the global variable and therefore uses the global variable, but compare this to the same line written “count = 1”. Then the line “global count” is absolutely needed according to scope rules. |
# Python program processing | |
# global variable | |
count = 5 | |
def some_method(): | |
global count | |
count = count + 1 | |
print(count) | |
some_method() | |
# Scope of Objects in Python : | |
# Scope refers to the coding region from which a particular Python object is accessible. Hence one cannot access any particular object from anywhere from the code, the accessing has to be allowed by the scope of the object. | |
# Let’s take an example to have a detailed understanding of the same: |
# Python program showing | |
# a scope of object | |
def some_func(): | |
print("Inside some_func") | |
def some_inner_func(): | |
var = 10 | |
print("Inside inner function, value of var:",var) | |
some_inner_func() | |
print("Try printing var from outer function: ",var) | |
some_func() |
Video of Namespace of Python
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND