Python modules
- A module is a python file generally ending with .py
- Create a new folder and create two files
- executable.py
- library.py
- Generally executable is a program which we run and libraries are also programs which will have reusable stuff.
- To execute a program use
python <filename>.py - To use functions from module into another you can import
- Style 1
import library
library.whoareyou()
print("this is executable")
- Import selectively
from library import whoareyou
whoareyou()
print("this is executable")
- Import with alias
from library import whoareyou as l_whoareyou
def whoareyou():
print("Im the executable")
whoareyou()
l_whoareyou()
dunder means special in python
-
In python when we see dunder (double underscores) means it has special functionality
-
__name__: This special variable holds the value__main__: when the python code was invoked directypython <filename>.py<module-name>: in the cases of import.
if __name__ == '__main__:
main()
Lets build a CLI Enabled Application in python
- This should be calculator
python calc.py add 1 2
# 3
python calc.py sub 1 2
# -1
python calc.py mul 1 2
# 2
# calc.py <operation> <number1> <number2>
- To do this we need to dive into standard library.
- To be discussed in next section
