Namespace are used to avoid ambiguity and provide uniqueness of scopes. That are useful for right decision of system scopes. this are make using two words. names (that are related to variable and object) and another are space (that are related its scopes).
Real Life Examples
For example, suppose you are job in multinational programming company. and there are working of lot of employee. Manager are announce the name of best programmer in the end of year. award goes to X? so what is this X. this is name of employee. company are lot of employee. so there can be possible to have two or more than two employee are same name. In this situation X name are not sufficient. then that are need more information about X such as surname and father name or address. That is a way to find right person are uniquely.
In similar manner programming are find ambiguity when exist same name of class, modules, function, and variable in a same scope. namespace is provide a solution to uniquely identities of name.
Types of namespaces
Namespace scopes is based on lifetime. system define namespace is start when python interpreter are start up. and it will be never delete its scopes.
local namespace is depending on functional scopes. when start execution of function it will start out scopes. and end of function execution this scopes are destroyed. for example in recursion case.
def increment(value): #base condition to stop recursion if(value==6): return None else : print(value) #display value value+=1 #increments value increment(value) #recursive call increment(0)
0 1 2 3 4 5
observe that when increment function call itself then with is create new namespace and scopes. and end the function execution is will destroyed.
Lifetime and scopes
Scope is define statically, but it can be used on dynamically at any time of program execution.
Use of non local variable
This is an simple case when define namespace not exist in local scope then it will find the namespace in non local scope. look at this situation.
auxiliary=10 def check_scops(): def read(): #read non local auxiliary print("read auxiliary",auxiliary) read() check_scops()
read auxiliary 10
Note that in this example auxiliary namespace not define local scope of within check_scops() function. In this situation python interpreter are check namespace are find outside namespace exist or not exist. if exist then uses.
But in this situation are read only access. because if try to modify non local namespace value then python interpreter will create new local namespace and assign that value to this. see this situation.
auxiliary=10 def check_scops(): def write(): #if modified value to non local space #then default it will create new namespace auxiliary=20 print("write auxiliary",auxiliary) write() check_scops() print("outside auxiliary",auxiliary)
write auxiliary 20 outside auxiliary 10
Note that in this example there are two same namespace but its scope are different. so how to modified outside namespace. in this situation we can used global keyword. see this example.
auxiliary=10 def check_scops(): def update(): #use global namespace global auxiliary #modified value auxiliary=20 print("update auxiliary",auxiliary) update() check_scops() print("outside auxiliary",auxiliary)
update auxiliary 20 outside auxiliary 20
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.