Writing reusable modules
For a module to be declared reusable, it has to have the following characteristics:
- Independent functionality
- General-purpose functionality
- Conventional coding style
- Well-defined documentation
If a module or package does not have these characteristics, it would be very hard, if not impossible, to reuse it in other programs. We will discuss each characteristic one by one.
Independent functionality
The functions in a module should offer functionality independent of other modules and independent of any local or global variables. The more independent the functions are, the more reusable the module is. If it has to use other modules, it has to be minimal.
In our example of mycalculator.py
, the two functions are completely independent and can be reused by other programs:
In the case of myrandom.py
, we are using the random
system...