I have written some Python in which some lines exceed 80 characters in length, which is a threshold I need to stay under. How can I adapt my code to reduce line lengths?
The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.
Zooming in to the point where 80 characters fits your whole screen at 1080p is very nice. This will ensure that the font size is large enough so smaller devices can read it, and more importantly, people will be able to read it without having to full screen your video.
PEP 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.
The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.
My current editor (Kate) has been configured to introduce a line break on word boundaries whenever the line length reaches or exceeds 80 characters. This makes it immediately obvious that I've overstepped the bounds. In addition, there is a red line marking the 80 character position, giving me advance warning of when the line is going to flow over. These let me plan logical lines that will fit on multiple physical lines.
As for how to actually fit them, there are several mechanisms. You can end the line with a \ , but this is error prone.
# works print 4 + \ 2 # doesn't work print 4 + \ 2
The difference? The difference is invisible-- there was a whitespace character after the backslash in the second case. Oops!
What should be done instead? Well, surround it in parentheses.
print (4 + 2)
No \ needed. This actually works universally, you will never ever need \ . Even for attribute access boundaries!
print (foo .bar())
For strings, you can add them explicitly, or implicitly using C-style joining.
# all of these do exactly the same thing print ("123" "456") print ("123" + "456") print "123456"
Finally, anything that will be in any form of bracket ((), []. {}), not just parentheses in particular, can have a line break placed anywhere. So, for example, you can use a list literal over multiple lines just fine, so long as elements are separated by a comma.
All this and more can be found in the official documentation for Python. Also, a quick note, PEP-8 specifies 79 characters as the limit, not 80-- if you have 80 characters, you are already over it.
If the code exceeding 80 chars is a function call (or definition), break the argument line. Python will recognise the parenthesis, and sees that as one line.
function(arg, arg, arg, arg, arg, arg, arg...)
If the code exceeding 80 chars is a line of code that isn't naturally breakable, you can use the backslash \
to "escape" the newline.
some.weird.namespace.thing.that.is.long = ','.join(strings) + \ 'another string'
You can also use the parenthesis to your advantage.
some.weird.namespace.thing.that.is.long = (','.join(strings) + 'another string')
All types of set brackets {} (dict/set), [] (list), () (tuples) can be broken across several lines without problems. This allows for nicer formatting.
mydict = { 'key': 'value', 'yes': 'no' }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With