Friday, April 24, 2020

Python Namespace


builtin namespace
Key
Value






global namespace


Key
Value




a
20
local Namespace


l
[1,2,3]
Key
Value




a
10




x
employee()




l
[a,b,c,d]



















Let’s first understand the above chart. Below are some findings.

1)    Namespace is dictionary which contains data in the form of key value pair.
2)    Key contains the variable and Value contains the object/data.

So what is namespace in conclusion:

It is mechanism to manage the variable names within the program, so that duplicate variable name won’t create any conflicts.
In the table, a and l is declared twice, one at global level, and one at local level.

What is Local namespace:
Names created within the function. This namespace is created every time function is called.  Its scope is till end of execution of function.

What is Global namespace:
Names imported from the module in the program. This namespace is created every time module is imported. Its scope is till the end of program.

What is built-in namespace:
This namespace covers built-in functions or names. This is created when the interpreter is started. Its scope is till the end of execution of program and it exits.
Scope in Python:

A local scope, also known as the innermost scope, holds the list of all local names available in the current function

A module level scope, it takes care of all the global names from the current module.

The outermost scope which manages the list of all the built-in names. It is the last place to search for a name that you cited in the program.


Scope Resolution in Python

Scope resolution for a given name begins from the inner-most function and then goes higher and higher until the program finds the related object. If the search ends without any outcome, then the program throws a NameError exception.


#Simple example

x=10 def m1(): print('the value of x in m1',x) m1() print('the value of x in main script',x)


1. A global variable is accessed inside the function m1.

Now lets check the global variables in this code.

x=10
def m1():
    print('the value of x in m1',x)
    print('Global variables in m1',globals())

m1()
print('the value of x in main script',x)
print('Global variables in main script',globals())

Output

PS F:\Movie\Books\Ignou\Python>  & 'E:\Users\shahid\AppData\Local\Programs\Python\Python37-32\python.exe' 'e:\Users\shahid\.vscode\extensions\ms-python.python-2021.5.926500501\pythonFiles\lib\python\debugpy\launcher' '57399' '--' 'f:\Movie\Books\Ignou\Python\LearningNamespace1.py'
the value of x in m1 10
Global variables in m1 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x006BDC10>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'f:\\Movie\\Books\\Ignou\\Python\\LearningNamespace1.py', '__cached__': None, 'x': 10, 'm1': <function m1 at 0x006F9420>}
the value of x in main script 10
Global variables in main script {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x006BDC10>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'f:\\Movie\\Books\\Ignou\\Python\\LearningNamespace1.py', '__cached__': None, 'x': 10, 'm1': <function m1 at 0x006F9420>}

In the above example

1. x=10 is global variable.
2. m1 is global variable.

Now setting the value of x=20
x=10
def m1():
    x=20
    print('the value of x in m1',x)
    print('Global variables in m1',globals())

m1()
print('the value of x in main script',x)
print('Global variables in main script',globals())

Output
PS F:\Movie\Books\Ignou\Python>  & 'E:\Users\shahid\AppData\Local\Programs\Python\Python37-32\python.exe' 'e:\Users\shahid\.vscode\extensions\ms-python.python-2021.5.926500501\pythonFiles\lib\python\debugpy\launcher' '59337' '--' 'f:\Movie\Books\Ignou\Python\LearningNamespace1.py'
the value of x in m1 20
Global variables in m1 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x006ADC10>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'f:\\Movie\\Books\\Ignou\\Python\\LearningNamespace1.py', '__cached__': None, 'x': 10, 'm1': <function m1 at 0x007B9420>}
the value of x in main script 10
Global variables in main script {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x006ADC10>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'f:\\Movie\\Books\\Ignou\\Python\\LearningNamespace1.py', '__cached__': None, 'x': 10, 'm1': <function m1 at 0x007B9420>}

In the above example there is NO CHANGE.

1. x=10 is global variable.
2. m1 is global variable.

Now declare x as global in side m1()

x=10
def m1():
    global x
    x=20
    print('the value of x in m1',x)
    print('Global variables in m1',globals())

m1()
print('the value of x in main script',x)
print('Global variables in main script',globals())

PS F:\Movie\Books\Ignou\Python>  f:; cd 'f:\Movie\Books\Ignou\Python'; & 'E:\Users\shahid\AppData\Local\Programs\Python\Python37-32\python.exe' 'e:\Users\shahid\.vscode\extensions\ms-python.python-2021.5.926500501\pythonFiles\lib\python\debugpy\launcher' '55337' '--' 'f:\Movie\Books\Ignou\Python\LearningNamespace1.py'
the value of x in m1 20
Global variables in m1 {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x009EDC10>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'f:\\Movie\\Books\\Ignou\\Python\\LearningNamespace1.py', '__cached__': None, 'x': 20, 'm1': <function m1 at 0x00AE9420>}
the value of x in main script 20
Global variables in main script {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x009EDC10>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'f:\\Movie\\Books\\Ignou\\Python\\LearningNamespace1.py', '__cached__': None, 'x': 20, 'm1': <function m1 at 0x00AE9420>}
PS F:\Movie\Books\Ignou\Python>

In the above example there value of x is changed.

1. x=20 is global variable.
2. m1 is global variable.



























No comments:

Post a Comment