319

Why in this millennium should Python PEP-8 specify a maximum line length of 79 characters?

Pretty much every code editor under the sun can handle longer lines. What to do with wrapping should be the choice of the content consumer, not the responsibility of the content creator.

Are there any (legitimately) good reasons for adhering to 79 characters in this age?

16
  • 94
    The answer to your question is in PEP-8.
    – cdleary
    Dec 31, 2008 at 21:03
  • 44
    Shorter line lengths enhance productivity by increasing your KLOC. :p
    – Alex
    Mar 2, 2013 at 1:00
  • 18
    Don't you people use side-by-side diff tools?
    – endolith
    Mar 3, 2016 at 1:49
  • 12
    For those who go as far back as punch cards, punch cards were 80 columns in width (en.wikipedia.org/wiki/…). That value was then adopted by dumb ASCII terminals. And then propagated into other "standards"...
    – RBV
    Nov 11, 2016 at 22:59
  • 3
    Black formats at 120 if you tell it to. I do. PEP-8 also says "it is okay to increase the line length limit up to 99 characters" but people seem to suppress that information much of the time.
    – NeilG
    Jul 26, 2019 at 0:18

9 Answers 9

178

Much of the value of PEP-8 is to stop people arguing about inconsequential formatting rules, and get on with writing good, consistently formatted code. Sure, no one really thinks that 79 is optimal, but there's no obvious gain in changing it to 99 or 119 or whatever your preferred line length is. I think the choices are these: follow the rule and find a worthwhile cause to battle for, or provide some data that demonstrates how readability and productivity vary with line length. The latter would be extremely interesting, and would have a good chance of changing people's minds I think.

15
  • 52
    Most reading studies are done in inches and not characters per line. The 66 character rule is based on studies done for reading newspapers. Recent studies have shown that when reading online articles, reading speed increases up to about 120 characters per line (10 inches at size 12 font) with no loss in comprehension.
    – Pace
    Mar 29, 2013 at 21:14
  • 13
    Actually everybody who read into that topic thinks that 79 characters is optimal. That's why it was added to PEP8! This answer is actually wrong. This one is the correct one
    – erikbstack
    Apr 29, 2013 at 11:24
  • 288
    there's no obvious gain in changing it to 99 or 119 or whatever your preferred line length is This is just so wrong in so many ways. Wrap a line at 40 characters and tell me how readable it is. Obviously less wrapping = more readability so long as you have the screen space, which in 2015, you do. Wrapping impacts readability. Readability impacts maintainability. Maintainability impacts quality. And quality is impacted if you're wrapping at 80 chars. Full stop.
    – Jonathan
    Jul 9, 2015 at 0:53
  • 11
    Arguing about readability with anything non-code is useless as those studies assume running text. Code looks completely different with different (character) line length every line. And even if you write till the end of the line, indentation changes the amount of characters per line.
    – Corvince
    Nov 7, 2017 at 12:46
  • 9
    PEP-8 also says "it is okay to increase the line length limit up to 99 characters" but people seem to suppress that information much of the time.
    – NeilG
    Jul 26, 2019 at 0:22
137

Keeping your code human readable not just machine readable. A lot of devices still can only show 80 characters at a time. Also it makes it easier for people with larger screens to multi-task by being able to set up multiple windows to be side by side.

Readability is also one of the reasons for enforced line indentation.

16
  • 84
    Yes, granted. But why 79? Why not 100 or 120? Keeping things readable works both ways. Too much up-and-down reading of code is hard to grok too.
    – pcorcoran
    Sep 18, 2008 at 0:39
  • 21
    It's true that a lot of devices can only show 80 characters. How many of them can't perform soft-wrapping?
    – Jim
    Sep 18, 2008 at 0:44
  • 41
    Also, it is preferred to not have code wrap. From a user experience perspective, it's unacceptable for most. Sep 18, 2008 at 7:00
  • 12
    79 comes from the days of dot matrix printers, they only printed 80 characters per line. Today we all sit with 16:9 ratio displays with a lot of width and limited vertical space... I follow every rule in PEP8 except this one, I believe it should be brought in line with current technology.
    – Gert Steyn
    Dec 3, 2013 at 14:00
  • 154
    79 characters also makes programmers use shorter more cryptic variable and function names to make everything fit. This is bad for readability.
    – Gert Steyn
    Dec 3, 2013 at 14:01
58

I am a programmer who has to deal with a lot of code on a daily basis. Open source and what has been developed in house.

As a programmer, I find it useful to have many source files open at once, and often organise my desktop on my (widescreen) monitor so that two source files are side by side. I might be programming in both, or just reading one and programming in the other.

I find it dissatisfying and frustrating when one of those source files is >120 characters in width, because it means I can't comfortably fit a line of code on a line of screen. It upsets formatting to line wrap.

I say '120' because that's the level to which I would get annoyed at code being wider than. After that many characters, you should be splitting across lines for readability, let alone coding standards.

I write code with 80 columns in mind. This is just so that when I do leak over that boundary, it's not such a bad thing.

4
  • 18
    "I write code with 80 columns in mind. This is just so that when I do leak over that boundary, it's not such a bad thing." Same for me.
    – KobeJohn
    Nov 4, 2011 at 6:27
  • 5
    10 years later: Doesn't that just depend on how you set up line wrapping. Line wrapping can be as intelligent or stupid as you want it. If it's uncomfortable to read that's just a failure of your editor. Sep 10, 2018 at 13:34
  • 7
    I code to 120 chars but occasionally longer when it suits readability. Black formats at 120 if you tell it to. PEP-8 also says "it is okay to increase the line length limit up to 99 characters" but people seem to suppress that information much of the time. Almost no-one uses terminals that are 80 wide. Log messages are never 80 wide.
    – NeilG
    Jul 26, 2019 at 0:25
  • I've been using tmux with vim for a few years now and I find it very flexible and powerful for viewing multiple panes of almost arbitrary line lengths, @jerub. Maybe you could look at getting a more powerful IDE?
    – NeilG
    Oct 16 at 2:48
46

I believe those who study typography would tell you that 66 characters per a line is supposed to be the most readable width for length. Even so, if you need to debug a machine remotely over an ssh session, most terminals default to 80 characters, 79 just fits, trying to work with anything wider becomes a real pain in such a case. You would also be suprised by the number of developers using vim + screen as a day to day environment.

5
  • <flame>Emacs FTW!</flame> +1, though. I think the 79 limit comes from the early days of UNIX (and possibly MULTICS) that had 80x25 character terminals.
    – Joe D
    Sep 10, 2010 at 13:55
  • 14
    My ssh+screen+vim environemnts have no problem displaying long lines. Apr 30, 2012 at 21:07
  • 65
    "66 characters per a line is supposed to be the most readable width for length" I suppose we should write code in 2 or 3 columns, since that's how newspapers are laid out? Dec 16, 2012 at 16:37
  • 29
    @mehaase: your sarcastic remark is quite close to the truth: decent editors can do split panes and show different stuff side-by-side (from same or different files). Coincidentally, this is usually only feasible when the code has a line-length standard...
    – mike3996
    Oct 21, 2015 at 12:57
  • this is the only answer that makes sense.
    – HAL9000
    Jun 7, 2022 at 7:24
21

Printing a monospaced font at default sizes is (on A4 paper) 80 columns by 66 lines.

5
  • 16
    I accept this standard; it's valid. But who prints code any more? Moreover, who prints code from an environment which is intolerant of scaling or other formatting options? When was the last time anyone you know was stumped by their inability to render a line of 100 characters?
    – pcorcoran
    Sep 18, 2008 at 4:04
  • 5
    Why do people print code in 2012? This reminds me of going to a technology conference and being handed a bag and a printed binder full of presentations. It's the 21st century people: e-mail me the slides or else that bag and binder are going straight into a landfill. Dec 16, 2012 at 17:03
  • 5
    so why 80-1 is better than 80-0 or 80-2?
    – n611x007
    Dec 12, 2014 at 15:41
  • 18
    "at default sizes" You say? Tell me more about these universally accepted default sizes. Mar 5, 2015 at 17:54
  • 29
    Yes, let's prioritize the way that code looks on printed paper above all else.
    – Jonathan
    Jul 9, 2015 at 0:59
16

Here's why I like the 80-character with: at work I use Vim and work on two files at a time on a monitor running at, I think, 1680x1040 (I can never remember). If the lines are any longer, I have trouble reading the files, even when using word wrap. Needless to say, I hate dealing with other people's code as they love long lines.

5
  • 1
    don't you use vim for javascript/html as well? Apr 20, 2018 at 15:45
  • 2
    @eladsilver I can't work out if that is a joke? :-D
    – Kyias
    Aug 24, 2018 at 8:39
  • sorry, not very profound with vim, obviously if you work in web you use it also for html/js and those types never come with a 80 char limit since front end developers don't know about pep8, so making python limit 80-char won't solve your problem if you use more then only python. so what I am asking is how do you handle other coding languages? Aug 24, 2018 at 20:00
  • I work in Vim with 120 character lines. I use :diffthis with horizontal split. If you can only fit 160 chars on 1680 pixels you must have a large font size.
    – NeilG
    Jul 26, 2019 at 0:26
  • I use vim and I hate 79 char rule. 95% of code it doesn't matter, but unit tests mocked data, urls and things like that can get out of hand. I rather have line wrap in this case.
    – webduvet
    Jun 16, 2022 at 22:22
5

Since whitespace has semantic meaning in Python, some methods of word wrapping could produce incorrect or ambiguous results, so there needs to be some limit to avoid those situations. An 80 character line length has been standard since we were using teletypes, so 79 characters seems like a pretty safe choice.

4
  • 5
    Most Python editors don't do soft word wrapping because it produces ambiguous hard to read code in a language where whitespace and indentation is important. Sep 18, 2008 at 1:03
  • 4
    It doesn't produce ambiguous or hard-to-read code so long as the wrapping is visually identified somehow. Kate does this and it works fine. If an editor doesn't handle this, then that's a reason to file a bug against the editor, not a reason to impose a coding style that avoids the bug.
    – Jim
    Sep 18, 2008 at 1:14
  • 6
    Even if it's indicated visually, it still makes the code much more difficult to read, which is why Python editors generally don't support it. Sep 18, 2008 at 1:16
  • Have you actually tried it for an extended period of time? I have. It doesn't make the code more difficult to read in my experience. Can you back up the claim that this is why Python editors don't include the feature? I've never heard that claim before.
    – Jim
    Sep 18, 2008 at 1:43
2

I agree with Justin. To elaborate, overly long lines of code are harder to read by humans and some people might have console widths that only accommodate 80 characters per line.

The style recommendation is there to ensure that the code you write can be read by as many people as possible on as many platforms as possible and as comfortably as possible.

1
  • 13
    This is a lazy argument. It is not always the case that 80 lines harms readability. A quick glance at any modestly complex Python codebase that wraps at 80 lines actually demonstrates the opposite - that wrapping single line function calls to several lines makes it harder to follow WTF is going on.
    – Jonathan
    Mar 14, 2015 at 0:46
-1

because if you push it beyond the 80th column it means that either you are writing a very long and complex line of code that does too much (and so you should refactor), or that you indented too much (and so you should refactor).

8
  • 107
    -1, I don't think you can categorically say that any line past the 80 char boundary requires a refactor. Class methods are already indented twice, add another indent for an "if", etc. and a simple list comprehension, and it's pretty easy to cross the 80-char boundary.
    – user297250
    Apr 4, 2011 at 19:17
  • 56
    Not to mention that if you name symbols in such a way that they are human readable, e.g. "users_directed_graph" instead of "usr_dir_gph", then even a simple expresswion will eat up quite a few characters per line. Dec 16, 2012 at 16:43
  • 10
    I have always found in Python that if I exceed 80 chars its wise to stop and think about why that is. Usually bad design decision is at fault.
    – Mike Vella
    Aug 10, 2013 at 0:44
  • 3
    This has been my experience as well. It also addresses longer variable names, as @mehaase points out, but I think this is a benefit. The available combinations of three consecutive words (in the case of "users_directed_graph") dwarfs the number of components that reasonably fit into a single namespace. I consider older code I've written where many similar long variable names are in the same namespace to be harder to read, and generally better to refactor. Nov 26, 2014 at 17:25
  • 5
    In a language that requires indents for each change of scope, saying that 80 character lines equates to complexity is an overly simplistic argument. Sometimes 80 characters is just what it takes to invoke a function. Modern IDE's/editors for other languages are smart enough to recognize this and can discern when to wrap as opposed to placing blanket restrictions on everything which harms readability overall.
    – Jonathan
    Mar 14, 2015 at 0:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.