It pays to be conventional

Python programmers have conventions for how they add comments to their programs. The conventions make the comments and code easier to read by other programmers.

Here are some of the guidelines for comments:

• Keep lines to fewer than 80 characters.

(This convention applies to the whole program, not just the comments.)

• Indent comments the same amount the code is indented.

For more information on indenting, see "Deciphering Code Blocks," later in this chapter.

• Use inline comments (comments that come after a line of code) sparingly, and only to explain things that aren't clear from reading the code.

The most important convention for comments is this one from the Style Guide for Python Code (http://www.python.org/dev/peps/pep-0008):

Comments that contradict the code are worse than no comments. Always make a priority of keeping the comments up-to-date when the code changes!

Was this article helpful?

0 0

Post a comment