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 ...

Get Python GUI Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.