I am an amateur programmer writing some python for a research project at my university. I need my code to be very readable for anyone who may be working on this project after me, and as such I am trying to follow PEP 8. I have run into a conflict of rules however. The offending lines are a dictionary definition after a long string of for loops and if statements. The problem is that convention has it that no line should be longer than 79 characters, yet line continuations should be indented from where they started from. I see three options of what could logically be done, but am unsure of what is best.
option 1: leave offending lines too long
def getIndexedData(directory): |
... |
... |
... |
# construct dictionary of images with peak locations |
peaks[image] = { |
'Xpixel': [float(x) for x in step[17][10][0].text.s|plit(' ')],
'Ypixel': [float(x) for x in step[17][10][1].text.s|plit(' ')]}
return peaks |
option 2: unindent continued lines
def getIndexedData(directory): |
... |
... |
... |
# construct dictionary of images with peak locations |
peaks[image] = { |
'Xpixel': [float(x) for x in step[17][10][0].text.split(' ')], |
'Ypixel': [float(x) for x in step[17][10][1].text.split(' ')]} |
return peaks |
option 3: split definitions somewhere (not sure where)
def getIndexedData(directory): |
... |
... |
... |
# construct dictionary of images with peak locations |
peaks[image] = { |
'Xpixel': |
[float(x) for x in step[17][10][0].text.split(' ')],|
'Ypixel': |
[float(x) for x in step[17][10][1].text.split(' ')]}|
return peaks |
I am also open to any other suggestions :)
Thanks,
~Aaron
A couple of relevent snippets from PEP 8 with my italics added.
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!
... it is okay to increase the nominal line length from 80 to 100 characters (effectively increasing the maximum length to 99 characters), provided that comments and docstrings are still wrapped at 72 characters.