Question 22: What is Namespace in Python ?
Answer: A Namespace in python is a space to hold unique names (object names) for a python program to avoid ambiguity of object names.
Now assume that in a sports academy their are three sports Hockey, Cricket and Tennis. And each sport has at least one member in team with same with other sports team. Suppose the name is "Ram".
Meaning name "Ram" is their in hockey ,Cricket and Tennis. Meaning there are three person of same name in sports academy. Now assume if a delivery boy want to deliver something to the person named "Ram" , But problem is that the delivery boy don't know the sport name of "Ram" (We have three ram's ). Means the situation here created a ambiguity of names. The delivery boy can avoid this ambiguity if he had know the sport name of person "Ram".
How the theory works in python ?
Python actually create namespace to each python module.
Now try to understand it with an example
There are two ways to import modules in python. 1. import module (And we get access to use all objects of imported module by using module.object )
**Note that in this case python creates a namespace and local object name have no conflict (ambiguity) with module name and module's object name.
#understanding namespace
import math
fac=math.factorial(6)
print(fac)
|
Output : 720
Now let's defined a new variable (or object) with the same name as module's function. (Here module - math and Function - Factorial) and see if the function name factorial overridden by variable name or not.
#understanding namespace
import math
factorial = lambda a : a*3
fac=math.factorial(6)
print(fac)
|
Output : 720
Note : Note that in this case whenever we want to use object or module's function we have to add math. before the function. Same as using sport name before name of person (Cricket.Ram) then there is no confusion at all. In this case function name of module is not overridden by variable (object) name.
So, in this case even if we have the same variable (objects) name as the function name of the Module, we will always get the right result because always because module name before the function name. Meaning python create a namespace for module in this case.
2. from module import objects (or function) (And we get access of imported function without module name )
#understanding namespace
from math import factorial
fac = factorial (6)
print(fac)
|
Output: 720
Now let's defined a new variable (or object) with the same name as module's function. (Here module - math and Function - Factorial) and see if the function name factorial overridden by variable name or not.
#understanding namespace
from math import factorial
factorial = lambda a : a*3
fac = factorial (6)
print(fac)
|
Output : 18
Note: In this case function name of module is not overridden by variable (object) name.
Question 23: What are types of Namespace and the lifetime of a namespace ?
Answer : Anything that you first write in python is actually print () function. We use print
() anywhere in a python program without importing and defining anything. help () and dir () also work as print (). These are built in namespace.
When we use a module and a module is created this is called global namespace. And when we define local function's variables it creates local namespace.
The Lifetime and Lifecycle of namespace:
The Lifetime and Lifecycle of namespace depends upon the scope of objects. Meaning if scope for a object ends the lifetime for the namespace also end.
It is simple. Meaning as long as we are using a namespace (objects and variables names) in a python program, it has lifetime.
Question 24: What is Scope in python and scope resolution ?
Answer : A scope is basically a block of code where a object/variable functions. In simple terms a object/variable useful within scope and after scope the lifetime of objects/variables end.
Now let's understand with an example
#scope in python
var= "global scope variable"
def func():
var= "local-scope variable"
print(var)
print(var)
func()
print(var)
|
Output:
Note: As you see in the above example the difference between local and global scope variable / objects. The Local variable/object (in the function) only function when we called the function (func) and after that the lifetime of local scope variable (namespace, name of variable- var) ends.
On the other hand global scope variable available throughout the code.
Now let's change the scope of local variable/ object to global variable/object.
#scope in python
var= "global scope variable"
def func():
global var
var= "local-scope variable"
print(var)
print(var)
func()
print(var)
|
Output:
Note : As you see in above example we can override the local variable and simply can use a local variable scope as global variable scope by using global keyword.
Scope Resolution: In some cases (for example in above examples) a scope have same variable name but function differently. In such cases python automatically draw a line between their functionality is called scope resolution.