Question

I've recently begun figuring it would be a good idea to follow PEP 8. I made my editor display the 80-column mark and am now attempting to wrap lines to fit in it. My question is this. In PEP 8 it says:

Limit all lines to a maximum of 79 characters.

Makes sense.

[...] For flowing long blocks of text (docstrings or comments), limiting the length to 72 characters is recommended.

Why 72? Why isn't 79 fine for docstrings or comments?

Was it helpful?

Solution

I believe it's a hangover from text documents:

At the end of the typewriter age, most designs were geared toward 72 CPL, derived from a pitch of 12 characters per inch, multiplied by 6 inches (see for example IBM Selectric). This would ensure at least 1 inch for each margin, with the U.S. government at the time having standardized on 8 1/2×11" paper.

Source

Many plain text documents still conform to 72 CPL out of tradition.

I think this is just as docstrings are often used in contexts outside of code, so it makes sense to conform to the style most plain text documents use to try and remain as consistent as possible. For example, the text of man pages are generally wrapped to 72 characters. By limiting docstrings to 72 characters, the output of help() mirrors this.

This question on Programmers may also be relevant.

OTHER TIPS

Docstrings are typically indented with their function, and start and end with triple quotes:

def foo(bar, baz):
    """Frobnicate the foobars into baz

    Parameters are ham and spammed.

    """

That's 79 - 4 - 3 = 72 characters left.

The reason that lines should be 79 characters is so that they can fit into an 80 character wide terminal (apparently they still exist). The reason that 72 characters are used on the docstring is that this means that you have equal padding on both sides (docstrings are indented, after all) and still fit into the 80 character width.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top