Python Namespace

Python Namespace and Scope of Variable

Before understand the namespace firstly we shound know the what is name ?

Name is an identitfier or a name given to objects.

Name is a way to access the property or objects.

As we know that in Python everything is an object.

For Example :
i=10

Where 10 is an object stored in memory and i is an identitfier or name is used to associated eith it.

Python provide built-in function like id() using this function wew can get the address of that object like below :

i=10
print(id(10)) # 2031417408

print(id(i)) # 2031417408

Here both refer the same object that means they have the same address or memory or id.

Lets take another example details :
def show():
    
    i=10
    print(id(i)) # 2031417408

    i=i+1
    print(id(i)) # 2031417568
    
    j=10
    print(id(j)) # 2031417408

show()

How it works internally :

In above image, object 10 is created and the identifier or name is i to hold this address associated with this i, when we increment by 1 it will create a new object or memory that is 11 and in this case 11 object associated with i.

But when j=10 is created or executed the new name j associated with 10 object. It means Python does not create a duplicate object.

In Python name can be refer to any type of object like as :

i=10
i=[1,2,3,4,5]
i="Hello Python"

All above are the valid name or identifier that refers to different type of objects.

In Python functions are also an objects so a name can be refer to them as well.

For Example :
def show():
    print("Welcome to Python")
    
x=show()

OR

x=show

x()

Here x can refer to a function or we can call a function using this name.

What is Namespace ?

Simply namespace is a group of identifiers or names.

A namespace contains all the built-in name is created when we start the Python interpreter and exists as well as long the interpreter runs that means we can use all the built-in names or function like id(), print(), quit() etc. are always available for developing a program in Python.

Each modules create its own global namespace and these namespace are isolated. Using namespace we can remove name collision.

A modules can have various functions or classes. A local namespace is created when a function is called similarly a namespace is created when a class is called.

Built Namespace
      |
Global Namespace
      |
Local Namespace

Python Variable Scope

A scope is a part of a program from where a namespace can be accessed directly.

Basically there are three type of scope :

  1. Local names
  2. Global names
  3. Built-in names

When a reference name or variable is declare inside a function that is called local namespace and when a reference name or variable is declare outside the function that is called global namespace.

For Example :
i=12 # global namespace

def display():
    i=100 # local namespace
    print(i)

display()
print(i)
Output is :

100
12

In this case when display function is call then print() function of display function print 100 and after display function print funtion print 12 because in this case local namespace can not modified global namespace value but if you want to change the global namespace value then you have to use global keyword like below :

i=12 

def display():
    global i
    i=100 
    print(i)
    
display()
print(i)
Output is :

100
100

In this case when display function is call then print() function of display function print 100 and after display function print funtion print 100.

If you want to access global namespace value from display function then you use globals() function like below :

i=12
    
    def show():
        
        #here it will create a new object that is different from above global variable
        i=20
        print("local value ", i)
        
        #if you want to get global variable then you use globals() function
        #and pass the global variable name to get the value
        
        x=globals()["i"]
        print(x)
            
show()
print(i)
Output is :

20
12
12

No comments: