Creating a reusable module
In Python, the module is the unit of software reuse. When we have a feature that must appear in more than one script, we'll put this feature into a module and import that module into each script that shares the feature.
It's important to note two slightly different senses of the word "reuse" as follows:
- We can define a class hierarchy to achieve localized reuse within an application. Inheritance is an elegant way to share code among related objects. Often we'll define all of these related classes in a single module file.
- We can define a module to achieve a less local reuse across applications.
To create a module that can be imported, we merely have to be sure that a Python file is visible in a directory that's part of the Python search path. Since the local directory is always visible, we can create a module simply by creating a file in the current working directory.
A module designed for import should consist mostly of import
, class
,...