views:

182

answers:

8

I think every Python code has seen PEP 8. The part that sticks out to me is:

Limit all lines to a maximum of 79 characters.

I'm sitting here on a widescreen monitor and coding right across the screen. I'm not coding in a terminal and don't plan on coding in a terminal. I therefor have no problems with character-line limits.

How many people actually follow this limit? Do you still follow it if you're not coding in a 80 character limit terminal? Is it bad that I don't follow it?

I hate how this restriction is apart of 'the style guide' for Python >.<

+4  A: 

I find it hard to read text that spans over 80 characters. My eye tends to lose the row while moving back to the left margin. So in a sense it is not a restriction due to having to view the code on a terminal (or a cmd window or xterm), but it is a readability mandate. I find myself breaking the rule by one or two characters at times, but overall I don't mind it. Also, I hardly ever have to use the \ continuation character, as I take advantage of the implicit continuation in lists.

Marco
I find the code looks confusing when line breaks are added at arbitrary places (like 79 characters). The PEP 8 `class Rectangle` example, for instance. I look at the first `if` statement and have to look over each line to find where the actual code block starts. There's three lines of `if` statement and one space added to the code block. Messes with my head.
brent
I find it easy to read that rather long if statement. The breaks are not consistent in line length, but they are consistent by always breaking with a conjunction operator. I too struggle with it. Also, with a wide screen monitor you can put two files side by side, and that is definitely a plus.
Marco
A: 

You can do whatever you want if it is your codebase. If it is someone else's then you have to play by their rules. Google for example has 2 character indents but PEP 8 says to use 4 spaces. I believe their is some quote from Guido about programming with 2 space indents by day and 4 spaces by night.

I like a character limit even with a widescreen monitor because then I can put frames of code side-by-side.

Code style is really all about personal preference. The important part is consistency. So write your python code anyway that makes you happy.

Evan
2-space indents are a minor form of obfuscation.
Glenn Maynard
And 4 space indents are a waste of space.
Nick Bastin
Let's settle on 3-space indents, then? :)
MaxVT
+1  A: 

As long as you don't have to scroll horizontally on your wide monitor (because I have seen that).

eumiro
Any decent programmer's editor is able to handle the occasional wrapped line cleanly without horizontal scrolling.
Glenn Maynard
IDE's decision on wrapping the long line is probably worse than the human decision.
eumiro
+4  A: 

Are you the only one who's going to read the code?

No matter what language you're programming in, it's recommended practice to keep code line length down. There are typically 2 types of causes for long lines:

  1. Deeply nested code: this type of code is hard to follow, especially if you have more than 2 levels of nesting. There is a tendency to miss else clauses when reading the code, or forgetting which else is for what if when reading longer functions. Try to break the code in several functions to improve readability.

  2. Complex expressions: like when you access a value from an object from an object from an object ... Or when you need to do a single operation on multiple values from 10 different places and you merge all the function calls and operators in a single line. You'll significantly improve the readability if you use temporary variables to split the logic into smaller segments that are easier to grasp. You should also look into this.

That being said, that PEP is just a guideline. Feel free to break it when you feel you're justified to do so. If you break it most of the time you need to reconsider the way you write code.

MihaiD
Breaking things into smaller functions is another very helpful technique. That includes using a series of generator expressions. Generator expressions can also be nicely spread out over a couple of lines — I usually line the `if` up with the `for`.
intuited
A: 

PEP8 is for humans but your program will run even if you do not follow it.

If you do not share your code and do not plan to do so, do whatever you want.

If you plan to share some part of your code someday, then you should follow PEP8. I mean, probably nobody will care if a few lines are 85 characters. But the code will be hard to read if it consistently is more than 200 characters wide. If you ever read a newspaper the same issue is involved when the text is formatted using columns.

The solution to the line length problem is probably neither arbitrary line break with continuation characters, nor using implicit lines continuation by enclosing some expressions inside parenthesis. It's probably to introduce intermediate variables and functions to have a code that logically breaks at less than 79 chars.

By the way you may want to stick to a still harder limit. I prefer 72 characters because I can allow one or two additional quoting levels inside 80 characters text mails. If not doing so the identiation will break at first quote.

kriss
+2  A: 

As with all style guides, it's just that, a guide. It's up to you whether or not you follow it. The main objective is consistency.

That said, I would recommend adopting an ~80 character limit for the following reasons.

  1. It makes complicated code easier to read.
  2. Get in the habit now for when you work on collaborative projects.
  3. It shows professionalism.
Garrett Hyde
“It shows professionalism.” — could you expand on what you mean by that? I tend to find that people invoke “professionalism” when they want others to do something, but don’t have an actual reason why they should.
Paul D. Waite
If you look at movie scripts, they all follow a specific format. So when you come across a script that doesn't follow this format, you assume the writer is an amateur. I view code the same way. When I see code that doesn't follow some rudimentary style guidelines, I automatically assume the coder doesn't have a lot of coding experience. So in this case, I define "professionalism" to mean 1) you know what you're doing, and 2) you understand general coding practices.
Garrett Hyde
+1  A: 

PEP 8:

But most importantly: know when to be inconsistent -- sometimes the style guide just doesn't apply. When in doubt, use your best judgment.

remosu
A: 

I set my editor to show me the 80-character limit line, and I use it as a warning, not a stop sign. If I can continue the line neatly to the next line before hitting the limit, I do so. However, if putting in a continuation makes it hard to read or makes it confusing, I have a long line. I won't make code harder to read just for the sake of a guide.

JerseyMike