/  Python Scope & Namespaces

Python Scope & Namespaces

Python Scope & Namespaces

In this tutorial, we will learn python in detail.


What is Name in Python?


Name (also called identifier) is just a name given to objects. The whole thing in Python is an object. Name is a way to access those objects.


For example, when we do the assignment a = 2, 2 is an object stored in memory and a is the name we associated with it. We can get the address of some object through the built-in function id().




a = 2
print('id(2) =', id(2))
print('id(a) =', id(a))




id(2) = 9302208
id(a) = 9302208


In the above example, both refer to the same object 2, so they have the same id().


What is a Namespace in Python?


A namespace is a collection of names. Different namespaces can co-exist at a given time but are totally isolated. When we start the Python interpreter a namespace containing all the built-in names is created and exists as long as the interpreter runs.

This is why built-in functions like id(), print() etc. are always available to us from any part of the program. Each module generates its own global namespace. These different namespaces are isolated. Hence, the same name that may exist in different modules do not coincide.

The built-in namespace encompasses global namespace and global namespace encompasses local namespace.


Python Scope & Namespaces


Depending on the scope of objects a lifetime of a namespace depends. The lifetime of that namespace ends as soon as the scope of an object ends. Therefore, it is not possible to access inner namespace’s objects from an outer namespace.




# 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 above example var1 is global namespace, var2 is the local namespace and var3 is the nested namespace.


But in some cases, programmer wants to access the global variable from anywhere. For this purpose, one should mark variable as global in order to access.




# Python program processing
# global variable

count = 5
def some_method():
   global count
    count = count + 1






Scope of Objects in Python:


The coding region from which particular Python object is accessible is referred to as scope. So, one cannot access any particular object from anywhere from the code, the accessing has to be allowable by the scope of the object.




# 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)
    print("Try printing var from outer function: ",var)




Inside some_func
Inside inner function, value of var: 10

Traceback (most recent call last):
 File "/home/1eb47bb3eac2fa36d6bfe5d349dfcb84.py", line 8, in
File "/home/1eb47bb3eac2fa36d6bfe5d349dfcb84.py", line 7, in some_func
   print("Try printing var from outer function: ",var)
NameError: name 'var' is not defined