Creating self-testing code using Python's __main__ section
Python comes with a very nice feature that enables each module to self-test. Making use of this feature is a great way of making sure that changes to our code do not break existing code and, additionally, the __main__
self-testing section can serve as documentation for how each module works.
Note
After a few months or years, we sometimes forget what our code is doing, so having an explanation written in the code itself is indeed a great help.
It is a good idea to always add a self-testing section to every Python module, when possible. It is sometimes not possible, but, in most modules, it is possible to do so.
Getting ready
We will extend the previous recipe, so, in order to understand what the code in this recipe is doing, we have to first read and understand the code of the previous recipe.
How to do it...
First, we will explore the power of the Python __main__
self-testing section by adding this self-testing section to our Resources...