Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you have a hard time keeping to 80 columns with Python? [closed]

Tags:

python

I find myself breaking strings constantly just to get them on the next line. And of course when I go to change those strings (think logging messages), I have to reformat the breaks to keep them within the 80 columns.

How do most people deal with this?

like image 780
ApplePieIsGood Avatar asked Feb 24 '09 18:02

ApplePieIsGood


People also ask

Does Python have a line length limit?

Maximum Line Length and Line BreakingPEP 8 suggests lines should be limited to 79 characters. This is because it allows you to have multiple files open next to one another, while also avoiding line wrapping. Of course, keeping statements to 79 characters or less is not always possible.

Why are there 80 columns?

Those requirement have mostly gone away now, but there are still valid reasons to keep the 80 column rule: To avoid wrapping when copying code into email, web pages, and books. To view multiple source windows side-by-side or using a side-by-side diff viewer. To improve readability.


1 Answers

I recommend trying to stay true to 80-column, but not at any cost. Sometimes, like for logging messages, it just makes more sense to keep 'em long than breaking up. But for most cases, like complex conditions or list comprehensions, breaking up is a good idea because it will help you divide the complex logic to more understandable parts. It's easier to understand:

print sum(n for n in xrange(1000000) 
             if palindromic(n, bits_of_n) and palindromic(n, digits))

Than:

print sum(n for n in xrange(1000000) if palindromic(n, bits_of_n) and palindromic(n, digits))

It may look the same to you if you've just written it, but after a few days those long lines become hard to understand.


Finally, while PEP 8 dictates the column restriction, it also says:

A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is most important.

But most importantly: know when to be inconsistent -- sometimes the style guide just doesn't apply. When in doubt, use your best judgment. Look at other examples and decide what looks best. And don't hesitate to ask!

like image 175
Eli Bendersky Avatar answered Nov 16 '22 03:11

Eli Bendersky