pep8 - How can I make my Python code stay under 80 characters a line? -


i have written python in lines exceed 80 characters in length, threshold need stay under. how can adapt code reduce line lengths?

my current editor (kate) has been configured introduce line break on word boundaries whenever line length reaches or exceeds 80 characters. makes obvious i've overstepped bounds. in addition, there red line marking 80 character position, giving me advance warning of when line going flow over. these let me plan logical lines fit on multiple physical lines.

as how fit them, there several mechanisms. can end line \ , error prone.

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

the difference? difference invisible-- there whitespace character after backslash in second case. oops!

what should done instead? well, surround in parentheses.

print (4 +      2) 

no \ needed. works universally, never ever need \ . attribute access boundaries!

print (foo     .bar()) 

for strings, can add them explicitly, or implicitly using c-style joining.

# of these same thing print ("123"     "456") print ("123" +      "456") print "123456" 

finally, in form of bracket ((), []. {}), not parentheses in particular, can have line break placed anywhere. so, example, can use list literal on multiple lines fine, long elements separated comma.

all , more can found in official documentation python. also, quick note, pep-8 specifies 79 characters limit, not 80-- if have 80 characters, on it.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -