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

332

u/hackflip Nov 14 '17

Use a linter tool like pyflakes or pylint on everything you write. Integrate them into your IDE. They will force you to be a better programmer.

47

u/mayhempk1 Nov 14 '17 edited Nov 14 '17

Not sure why this is being downvoted, linters are very important and powerful tools. They aren't quite as good as learning to write good code in the first place, but they can be very useful for debugging.

edit: His comment was -3 now it's +20, oops

→ More replies (3)

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...

→ More replies (21)

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.

→ More replies (4)

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)
→ More replies (5)
→ More replies (9)

20

u/ic_97 Nov 14 '17

Python noobie care to explain whats a linter?

38

u/[deleted] Nov 14 '17

https://en.wikipedia.org/wiki/Lint_(software)

Generically, lint or a linter is any tool that detects and flags errors in programming languages, including stylistic errors.

18

u/WikiTextBot Nov 14 '17

Lint (software)

In computer programming, lint is a Unix utility that flags some suspicious and non-portable constructs (likely to be bugs) in C language source code. Generically, lint or a linter is any tool that detects and flags errors in programming languages, including stylistic errors. The term lint-like behavior is sometimes applied to the process of flagging suspicious language usage. Lint-like tools generally perform static analysis of source code.


[ ^PM | Exclude ^me | Exclude from ^subreddit | FAQ / ^Information | ^Source | ^Donate ] Downvote to remove | v0.28

8

u/lonely_ent_guy Nov 14 '17

good bot

6

u/iceardor Nov 14 '17

lonely bot

10

u/blahsphemer_ Nov 14 '17

Much better than a owner of a broken bot

→ More replies (1)

14

u/NoLemurs Nov 14 '17

To add to /u/lolshovels answer, the main linters you might want to look at are pep8 pyflakes (flake8 which combines those two), and pylint.

flake8 is great if you want a fairly lightweight tool that still does a lot for you without a lot of configuration and doesn't generate a lot of noise. pylint is much heavier and slower and will generate a ridiculous amount of output by default - it requires a lot of configuration to actually be useful, but can be used to give you much more specific and detailed feedback if you want it.

Personally I use flake8 because I like more or less instantaneous feedback. Both are worth trying though.

→ More replies (17)

7

u/pcp_or_splenda Nov 14 '17

a program that helps you conform to PEP8, in the case of python.

→ More replies (3)
→ More replies (1)

4

u/AstroPhysician Nov 14 '17

a linter should be part of your workflow already. We don't allow any code to be pushed unless there are no pylint warnings

→ More replies (4)