Python and PEP8 - Recommendations on breaking up long lines?
thebalancepro at gmail.com
Thu Dec 5 02:47:01 CET 2013
I use PyCharm which is set up by default to warn when line length exceeds 120 chars, not 80. Perhaps times have changed?
I often break comprehensions at the for, in and else clauses. It's often not for line length reasons but because it's easier to follow the code that way. I have heard this is how Haskell programmers tend to use comprehensions (comprehensions are from Haskell originally):
if loc['fields']['provider_id'] == provider_id])))
The other suggestion I have is to put the with clauses in a generator function. This saves you a level or more of indentation and modularises the code usefully. Here's an example:
with open(csv_filename) as csv_file:
for csv_row in list(csv.DictReader(csv_file, delimiter='\t')):
then invoked using:
for row in spreadsheet("...")
# your processing code here
More information about the Python-list