What does it means to not exceeding 80 characters in a line in Python?

Question:

There is a mention that it is preferable for that a line in a Python script should not exceed more than 80 characters. For example:

print("A very long strings")

Does it include the characters of the print function, the strings (including spaces), parenthesis etc.?

And does it not limited to other functions besides print?

What is the reason behind this? Already googled for answers but still can’t really get the picture.

Answers:

Yes, it includes anything that takes up space and is not limited to any function

Usually, shells can only display 80 characters in one line before moving onto the next line. If you were formatting things in the CLI, having more than 80 characters from a print statement would offset all of the formatting. This is because they follow PEP8 standards.

Answered By: ChaiNunes

According to PEP8 standard:

Limiting the required editor window width makes it possible to have
several files open side-by-side, and works well when using code review
tools that present the two versions in adjacent columns.

It’s a good idea to follow PEP8 convention when you code by python.

Answered By: Mehran Meidani

What is the length?

Everything counts towards the width.

The "standard"

PEP8 is a recommendation not a law.
It explains the reasons pretty nicely:

  • several files open in an editor side-by-side
  • code review tools

Also, up to 99 characters are explicitly "allowed".

Maximum Line Length

Limit all lines to a maximum of 79 characters.

For flowing long blocks of text with fewer structural restrictions (docstrings or comments), the line length should be limited to 72 characters.

Limiting the required editor window width makes it possible to have several files open side-by-side, and works well when using code review tools that present the two versions in adjacent columns.

The default wrapping in most tools disrupts the visual structure of the code, making it more difficult to understand. 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. Some web based tools may not offer dynamic line wrapping at all.

Some teams strongly prefer a longer line length. For code maintained exclusively or primarily by a team that can reach agreement on this issue, it is okay to increase the nominal line length from 80 to 100 characters (effectively increasing the maximum length to 99 characters), provided that comments and docstrings are still wrapped at 72 characters.

Practical tips

Besides the line continuation with , you can always use multiple lines as soon as parenthesis are involved. In fact, it is consider more Pythonic.

For example, you can re-write this:

def func(very_long_name_1, very_long_name_2, very_long_name_3, very_long_name_4):
    pass

into this:

def func(very_long_name_1, very_long_name_2, 
         very_long_name_3, very_long_name_4):
    pass

or this:

def func(very_long_name_1, 
         very_long_name_2, 
         very_long_name_3, 
         very_long_name_4):
    pass

The same applies to function calls.

Also, lists (or tuples or dictionaries) can be defined on multiple lines:

my_list = ['string_1',
           'string_2',
           'string_3']

Applied right, this style improves readability.

Answered By: Mike Müller

It includes everything on the line.

The reasoning is that very long lines hinder clarity and readability.

To use an example from the python code style guide, which is easier for you to read?

with open('/path/to/some/file/you/want/to/read') as file_1, open('/path/to/some/file/being/written', 'w') as file_2:
    file_2.write(file_1.read())

or

with open('/path/to/some/file/you/want/to/read') as file_1, 
     open('/path/to/some/file/being/written', 'w') as file_2:
         file_2.write(file_1.read())

One other reason is historical. The style guide in the same section I link to above mentions a 72, 79, and 80-100 character limit for various situations. These are the column lengths of a punch card.

Answered By: ConnectedSystems
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.