9.4 Commenting Code

Commenting is a practice that should not be overlooked by new users of Python. As easy as it is to write a few lines of script, it takes only a few more seconds to quickly document what those lines do. This comes in handy in a multitude of situations such as:

Sharing code and projects with other programmers

Maintaining code in older projects

Changing the functionality inside of a re-usable component

It may seem frivolous when scripts are brief, but it is a habit that should be solidified from day 1. Look at the example code below:

This script is hard to read for a number of reasons. The main reason is that its actions weren't obvious when quickly skimmed. One quick way to increase the readability of this script is to comment it. Let's take the above script and add some basic comments to it: