Modules, Libraries, Packaging, and Code Re-Use
This one is pretty visual. The big takeaway: writing code not just as individual one-off scripts, but as collections of modules and submodules that can refer to one another.
Practice
Refactor the rps.py
script into a module called rps_package
:
$ tree rps_package
rps_package
├── __init__.py
├── __main__.py
├── rps.py
└── test_rps.py
Where __main__.py
will become the entry point when we run the code:
# __main__.py
from rps_package.rps import main
main()
Meaning that we can run the code as a Python module:
$ python3 -m rps_package
… which parallels the behavior of libraries like unittest
:
$ python3 -m unittest