Is it bad that I don't follow PEP 8 and cut my lines at 79 characters?
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开发者_C百科 guide' for Python >.<
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.
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:
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.
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.
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.
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.
No Way.
✔ My Arguments:
- Anything beyond 80 chars is part of less important logic. (If needed people can scroll right)
- Enforcing this rule makes those unimportant code come in front of my eyes to clutter
- One has to scroll vertically for huge distances all the time than horizontal scrolling occasionally
- Most modern editors have soft-wrap feature that automates this for any choice of length limit (not just 80)
- Soft-wrap in modern editors is a keystroke-away, it means is one could get best of both the worlds if lines were left as they are.
✔ Linus Torvalds' Arguments:
grep
or 'find in files' will fail strings are severed at unintended location.Vertical limit(VT100 terminal) is more painful than 80 char horizontal limit
✔ The verdict:
It's like try-
ing to read
a news arti-
cle written
like this.
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.
As long as you don't have to scroll horizontally on your wide monitor (because I have seen that).
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.
- It makes complicated code easier to read.
- Get in the habit now for when you work on collaborative projects.
- It shows professionalism.
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.
精彩评论