Create modules


A module is just a Python file with a set of functions that us usually not used by itself. For example the "my_calculator.py".

examples/modules/my_calculator.py
def add(a, b):
    return a + b
A user made module is loaded exactly the same way as the built-in module. The functions defined in the module are used as if they were methods with the dot-notation.

examples/modules/add.py
import my_calculator

z = my_calculator.add(2, 3)

print(z)  # 5
We can import specific functions to the current name space (symbol table) and then we don't need to prefix it with the name of the file every time we use it. This might be shorter writing, but if we import the same function name from two different modules then they will overwrite each other. So I usually prefer loading the module as in the previous example.

examples/modules/add_from.py
from my_calculator import add

print(add(2, 3))  # 5

examples/modules/add_as.py
import my_calculator as calc

z = calc.add(2, 3)

print(z)  # 5