How can I make Python code stay under 80 characters?

I wrote some Python in which some lines exceed 80 characters in length, which is the threshold that I should remain. How can I adapt my code to shorten the length of the lines?

+52
python pep8
Jan 15
source share
4 answers

My current editor (Kate) has been configured to introduce line breaks at word boundaries whenever a line reaches or exceeds 80 characters. From here it immediately becomes apparent that I have stepped over the boundaries. In addition, there is a red line indicating the position of 80 characters, giving me a preliminary warning about when the line will flow. This allows me to plan logical lines that will correspond to many physical lines.

As for how to properly adjust them, there are several mechanisms. You can end the line with \, but this is a mistake.

# works print 4 + \ 2 # doesn't work print 4 + \ 2 

Difference? The difference is invisible - after the backslash in the second case, there was a space character. Unfortunately,

What should be done instead? Well, surround it in parentheses.

 print (4 + 2) 

Not necessary. It really works all over the place; you'll never need it. Even for the boundaries of access to attributes!

 print (foo .bar()) 

For strings, you can add them explicitly or implicitly using a C-style join.

 # all of these do exactly the same thing print ("123" "456") print ("123" + "456") print "123456" 

Finally, anything that will be in any form brackets ((), []. {}), And not just in brackets, in particular, can have a line break anywhere. So, for example, you can use a list literal over multiple lines just fine if the items are separated by a comma.

All of this and more can be found in the official documentation for Python. In addition, the quick note PEP-8 defines 79 characters as a limit, not 80-- if you have 80 characters, you have already completed it.

+57
Jan 15 '10 at 10:14
source share
โ€” -

If code exceeding 80 characters is a function call (or definition), break the argument string. Python recognizes the parentheses and sees this as a single line.

 function(arg, arg, arg, arg, arg, arg, arg...) 

If code exceeding 80 characters is a line of code that is not naturally destructible, you can use the backslash \ to "escape" the new line.

 some.weird.namespace.thing.that.is.long = ','.join(strings) + \ 'another string' 

You can also use parentheses to your advantage.

 some.weird.namespace.thing.that.is.long = (','.join(strings) + 'another string') 

All types of brackets {} (dict / set), [] (list), () (tuples) can be divided into several lines without problems. This allows for better formatting.

 mydict = { 'key': 'value', 'yes': 'no' } 
+20
Jan 15 '10 at 10:09
source share

Idiomatic Python says:

Use backslash as a last resort

So, if you use parentheses () , avoid backslashes. If you have a.train.wreck.that.spans.across.a.dozen.cars.and-multiple.lines.across.the.whole.trainyard.and.several.states() do something like:

 lines = a.train.wreck.that.spans.across.a.dozen.cars.and-multiple.lines lines.across.the.whole.trainyard.and.several.states() 

Or, preferably, reorganize your code. You are welcome.

+18
Jan 15 '10 at 10:23
source share

I would add two points to the previous answers:

Lines can be automatically combined, which is very convenient:

 this_is_a_long_string = ("lkjlkj lkj lkj mlkj mlkj mlkj mlkj mlkj mlkj " "rest of the string: no string addition is necessary!" " You can do it many times!") 

Note that this is effective: it does not result in the string concatenation calculated when the code runs: instead, it is directly considered as one long string literal, therefore it is efficient.

A little caveat regarding Devin's answer: the syntax of the "parentheses" does not actually "work universally." For example, d [42] = "H22G" cannot be written as

 (d [42] = "H2G2") 

because parentheses can only be used around a โ€œcomputedโ€ expression (this does not include assignment (=) as above).

Another example is the following code that generates a syntax error:

 with (open("..... very long file name .....") as input_file): 

In fact, parentheses cannot be placed around statements in a more general sense (expressions only).

In these cases, you can use the syntax "\" or, which is better (since "\" should be avoided, if possible), split the code into several statements.

+18
Jan 15
source share



All Articles