Python Packages
Python Packages
math.py).
Package: A directory containing modules and a special
__init__.py file.
Sub-Packages: Packages nested within other packages for
deeper organization.
math_operations/calculator.py:
This calculate file is a simple placeholder that prints
“Performing calculation…”, serving as a basic demonstration or
utility within the package.
def calculate():
print("Performing calculation...")
math_operations/basic/__init__.py:
This __init__.py file initializes the basic sub-package by
importing and exposing the add and subtract functions from
their respective modules (add.py and sub.py). This makes these
functions accessible when the basic sub-package is imported.
return a + b
math_operations/basic/sub.py:
return a - b
In the same way we can create the sub package advanced with
multiply and divide modules.
from math_operations import calculate, add, subtract
Output:
6
8