Nearby lessons
97 of 108Python - Namespaces
Detailed Tutorial Notes
The main advantage of explicit module reloading is we can ensure that updated version is
always available to our program.
Finding members of module by using dir() function:
Python provides inbuilt function dir() to list out all members of current module or a
specified module.
dir() ===>To list out all members of current module
dir(moduleName)==>To list out all members of specified moduleEg 1: test.py
main.py
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__nam
e__', '__package__', '__spec__', 'f1', 'x', 'y']
Eg 2: To display members of particular module:
durgamath.py:
1) x=8882)
3) def add(a,b):
4) print("The Sum:",a+b)5)
6) def product(a,b):
7) print("The Product:",a*b)test.py:
main.py
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'add', 'product', 'x']
Note: For every module at the time of execution Python interpreter will add some special
properties automatically for internal use.
Eg: __builtins__,__cached__,'__doc__,__file__, __loader__, __name__,__package__,
__spec__
Based on our requirement we can access these properties also in our program.
Eg: test.py:
main.py
<module 'builtins' (built-in)> None None
test.py
1) <_frozen_importlib_external.SourceFileLoader object at 0x00572170>
2) __main__
3) None
4) NoneThe Special variable __name__:
For every Python program , a special variable __name__ will be added internally.
This variable stores information regarding whether the program is executed as an
individual program or as a module.
If the program executed as an individual program then the value of this variable is
__main__
If the program executed as a module from some other program then the value of this
variable is the name of module where it is defined.
Hence by using this __name__ variable we can identify whether the program executed
directly or as a module.