This leads me to question whether a newline character counts towards the limit. I attempted to solve my question by looking up the definition of a line in IEEE Std 1003.1-2024 (POSIX Issue 8). (POSIX due to wide acceptance of its definitions.)
A strict adherence to POSIX would make the newline character count towards the 79 characters limit, but it possible to argue that a âLineâ in PEP-8 is to be understood as a âDisplay Lineâ.
Which is it?
Relevant definitions follow.
3.185 Line
A sequence of zero or more non- characters plus a terminating character.
3.58 Character
A sequence of one or more bytes representing a member of a character set.
3.108 Display Line
A line of text on a physical device or an emulation thereof. Such a line has a maximum number of characters which can be presented.
3.224 Newline Character ()
A character that in the output stream indicates that printing should start at the beginning of the next line. It is the character designated by â\nâ in the C language. It is unspecified whether this character is the exact sequence transmitted to an output device by the system to accomplish the movement to the next line.
3.57 Carriage-Return Character ()
A character that in the output stream indicates that printing should start at the beginning of the same physical line in which the carriage-return occurred. It is the character designated by â\râ in the C language. It is unspecified whether this character is the exact sequence transmitted to an output device by the system to accomplish the movement to the beginning of the line.
PEP 8 is a guideline, not a strict rule, and only matters if you choose to apply it to your code. When you make that choice, you can decide on your preferred interpretation at the same time.
Youâre overthinking this. If youâre at the edge, wrap the line.
Note that PEP 8 says things like:
Some other good reasons to ignore a particular guideline:
When applying the guideline would make the code less readable, even for someone who is used to reading code that follows this PEP.
[âŠ]
and:
For flowing long blocks of text with fewer structural restrictions (docstrings or comments), the line length should be limited to 72 characters.
But anyway, relevant to your question is this:
The limits are chosen to avoid wrapping in editors with the window width set to 80, even if the tool places a marker glyph in the final column when wrapping lines.
The 80th column is for the â marker. Hence, the 79 limit is indeed meant for what that POSIX standard calls âDisplay Lineâ.
The line width has to do with human consumption, which should hint what to do with both newlines and tabs. 80 characters isnât magical except historically, but âexpertsâ claim that 50-80 character lines are best for code reading - keeps you from scanning back and forth so much. Shorter is better for humans, but 50 characters is not super-practical in languages which rely on indentation for visually distinguishing code elements - Python even makes it part of the syntax - so 80 is more practical. And as others have said, itâs a guideline, not a set of rules.
The 79 is intended for 80-column-wide terminals. There is (or was?) some ambiguity in the interpretation of what should happen if you write 80 regular characters plus a newline â sometimes youâd get a spurious blank line.
I definitely meant to count only printable characters, not the newline at the end.
And tabs expanded using 8 spaces per tab. All other control characters are undefined or illegal. I am at heart a Unix person and assumed CR+LF (\r\n) would be translated to \n (LF, per Unix convention). Barry Warsaw alone will remember what Ctrl-L (FF, \f?) did â it was an Emacs feature.
I was just about to say roughly the same thing, except that it also applied to printouts.
A similar problem could occur if you wanted to print a page and then follow it with FF (Form Feed) to eject the page and/or start a new page. If you printed the exact number of lines that fitted on the page, youâd then be at the start of the next page, and FF would lead to an unwanted blank page.
In the vt100 family world you would print 80 columns of text then CR and LF to go to the next line.
There where none vt100 terminals that when you print in the 80th column might do an implicit CR-LF for you. But itâs been decades since I encounter one of those.