Namespaces and Scoping in Python
Namespaces and Scoping in Python
Names of the variables having the mapping with the objects, where a namespace is a dictionary of object and variables names. Python language has the right to access the variables in the global namespace as well as local namespaces. In python, each function is declared to have its local namespaces and all classes methods generally follow the same scoping rule as ordinary function respectively.
In Python, if any variables are assigned a value in a function called to be local and if you want to assign the value to the global variable inside the function then you must use the global statement accurately.
Example:
yes = 2
def Addyes():
# do uncomment the following line to fix the code:
# global yes
yes = yes + 1
print (yes)
Addyes()
print (yes)
Reload Function:
The working of this function can be explained in such a way that when the module is imported into the program in python, then the code of the top-level section of the module will be executed only once. Therefore if you want to execute the top-level module, you have to use the reload() function. Here reload() function generally imports the previously imported modules again in code.
Example:
import lib12
lib12.reload(module)
Packages in python language:
A package is just like folders in computers. Packages are complete hierarchical file directory structure which is defined in the single python application that composites of sub-packages, modules, etc.
Example:
#Save this function as sun.py
def sun():
print "I'm sun"
#import sun.py in this program from package
import sun
sun.sun()
