r/Python • • Nov 14 '17

Senior Python Programmers, what tricks do you want to impart to us young guns?

Like basic looping, performance improvement, etc.

1.3k Upvotes

629 comments sorted by

View all comments

Show parent comments

76

u/vosper1 Nov 14 '17

This, but you don't have to be a stickler about the 80 char line limit from PEP8. We have wide screens these days. I find 120 chars to be a nice number.

21

u/kourckpro Nov 14 '17

Specific checks can be ignored e.g. with a .pylintrc file.

12

u/tetroxid Nov 14 '17

Narrow files help when diffing / merging on a laptop.

Also you can open two or three files side by side, or a file and a webpage, and so on.

44

u/28f272fe556a1363cc31 Nov 14 '17

I disagree. If you follow the 80 character rule you can have two source code files open next to each other. On the other screen you cam have documentation and a Web page or project readme.

79

u/vosper1 Nov 14 '17

I can easily have two files open next to each other with a 120 char limit, and a web page, on my modern ~$300 monitor. IMO 80 characters encourages less descriptive variable names and/or artificial breakup of code purely to meet that character limit. 120 chars gives you a bit more room, without making things too long.

15

u/Kevin_Clever Nov 14 '17

I program sometimes on upright screens. More lines at a glance. 80 Chars don't fit twice then though...

9

u/iceardor Nov 14 '17

How many people develop with a single ≤1920x1200 monitor? I am inefficient and claustrophobic with less than 2 ≈1920x1080 monitors.

22

u/kaihatsusha Nov 14 '17

Hop onto a few servers with ssh and vi, or take your laptop to work alongside a client. Plenty of reasons to stick with the 80.

7

u/IcefrogIsDead Nov 14 '17

it gets me stressed when i have to alt tab 3 times to write a line of code

7

u/[deleted] Nov 14 '17

laptops, for when you're not in the office

→ More replies (2)

6

u/ingolemo Nov 14 '17

raises hand. My monitor is 1440x900.

→ More replies (4)

3

u/shif Nov 14 '17

when i'm not at the office I work with only my laptop screen which is 1366x768, surprisingly I got used to it, at work i have 2 extra monitors, one is 1920x1080 and the other is a vertical usb monitor that is 768x1366

2

u/Corm Nov 15 '17

4k user checking in. I still gun for 80 but it's no big deal since vim ships smart wrapping via set breakindent

→ More replies (6)
→ More replies (2)

9

u/Airith Nov 14 '17

Yep, I usually have file structure plus two files open in Sublime, that 80 char limit is necessary for 1080p, if I had more than one 1440p monitor I could raise the char limit.

I feel that 80 isn't just for fitting on monitors, it's for keeping code in an easily digestible amount.

3

u/[deleted] Nov 14 '17

If you follow the 80 character rule you can have two source code files open next to each other

I don't know about you, but my single monitor is more than capable of displaying > 160 characters worth of pixels in a row. The 80 character guideline is a relic of old terminals.

→ More replies (1)
→ More replies (2)

15

u/NoLemurs Nov 14 '17

I would suggest a different rule.

If you have a line that's more than 80 characters, rewrite it to be less than 80 by whatever means works best (including introducing intermediate variables, or shorter variable names if necessary).

Forcing yourself to actually write the short version prevents you from being lazy. If the line was better at 100+ characters, then feel free to go back to the 100+ character version, but I have found that 99 times out of 100 the shorter version is more readable regardless of how ok I was with the longer version.

16

u/[deleted] Nov 14 '17

80 is pretty extreme. I agree we shouldn't be going nuts. But if I'm putting a log message in a block that is 12 characters indented (not a totally uncommon concept) I have around 60 characters to create an appropriately descriptive log message.

Using variables and string formatting gives me actually less space, and isn't as readable as just the raw string, and isn't a good practice if I'm only calling those variables a single time in that log message.

Escaping the line breaks in the string is a disaster on readability. And shortening variable names often leads to ambiguous or bad variables.

So not making lines any longer than they have to be is a good practice.

Having a character limit, I think is generally unnecessary, and so commonly ignored that it's an almost worthless portion of the PEP.

17

u/NoLemurs Nov 14 '17 edited Nov 14 '17

Escaping the line breaks in the string is a disaster on readability.

Generally there's no need to do any escaping. Python naturally continues multiline strings. For instance:

long_string = ("YOU don't know about me without you have read a book by the name "
    "of The Adventures of Tom Sawyer; but that ain't no matter. That book was "
    "made by Mr.  Mark Twain, and he told the truth, mainly. There was things "
    "which he stretched, but mainly he told the truth. That is nothing. I "
    "never seen anybody but lied one time or another, without it was Aunt "
    "Polly, or the widow, or maybe Mary.  Aunt Polly - Tom's Aunt Polly, "
    "she is - and Mary, and the Widow Douglas is all told about in that "
    "book, which is mostly a true book, with some stretchers, as I said "
    "before.")

That's perfectly valid python which defines a single string, and is much more readable than the single line version would be. No escapes needed.

If your string is much longer than the above, I'd encourage you to write it to a file and read it in at run time.

As for shortening variable names, I haven't found much conflict there. If your variable name is more than about 15 characters, the sheer length of the variable name is hurting readability, and there's probably something you could be doing better. I've read enough Java to be pretty confident that your 30+ character descriptive variables do not make the code more readable.

EDIT: Add parentheses.

21

u/kindall Nov 14 '17

You need parentheses around that value to get it to behave the way you say it behaves.

→ More replies (1)

3

u/iceardor Nov 14 '17

My longest lines are from logging, too. I've tried to shorten them but nothing is satisfyingly readable, so I go back to the more readable long version.

The closest I've come to being happy with long strings is ' '.join([...]) or '\n'.join([...]), or writing my logging message in triple quotes like a docstring and using textwrap and lstrip('\n')to remove leading whitespace.

Fstrings will help with formatting args 'Reason: {reason}'.format(reason=reason) is ridiculously redundant. I could get away with 'Reason: {}'.format(reason) here, but as the message gets longer and has more format variables, using named format variables is critical to readability and not accidentally transposing or skipping a format variable. Formatting using 'Reason: {reason}'.format(**locals()) or some inspect reflection seems too hacky.

→ More replies (4)

3

u/ergzay Nov 14 '17

I disagree. People like to split code into multiple vertical columns to open multiple files at once. If it's wider than 80 characters then this limits the number of parallel open files.

2

u/KlaireOverwood Nov 14 '17

Except PEP8 doesn't limit you to 80:

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.

The Python standard library is conservative and requires limiting lines to 79 characters (and docstrings/comments to 72).

2

u/stevenjd Nov 15 '17

This, but you don't have to be a stickler about the 80 char line limit from PEP8. We have wide screens these days. I find 120 chars to be a nice number.

The 80 character limit has nothing to do with the width of the screen. It has to do with readability. Something like 70-90 characters is the upper limit for maximum readability, beyond that it is harder and slower for the eyes to track across the line without error.

You can probably get away with it if only a small proportion of lines reach 120 characters, say less than 10% of the code and no more than four in a row, but if you're reading code that regularly hits the 120 character limit, your reading is probably suffering, and the code probably sucks.

But then, if you're regularly hitting 80 columns, the code probably sucks too:

etc. Wide code is a code smell.

→ More replies (1)
→ More replies (4)