Python and PEP8 - Recommendations on breaking up long lines?

Terry Reedy tjreedy at udel.edu
Wed Nov 27 22:03:11 EST 2013


On 11/27/2013 9:03 PM, Victor Hooi wrote:
> Hi,
>
> Also, forgot two other examples that are causing me grief:
>
>      cur.executemany("INSERT INTO foobar_foobar_files VALUES (?)",
>                      [[os.path.relpath(filename, foobar_input_folder)] for filename in filenames])
>
> I've already broken it up using the parentheses, not sure what's the tidy way to break it up again to fit under 80? In this case, the 80-character mark is hitting me around the "for filename" towards the end.

add another linebreak, you are still inside the parens.

> and:
>
>                      if os.path.join(root, file) not in previously_processed_files and os.path.join(root, file)[:-3] not in previously_processed_files:
>
> In this case, the 80-character mark is actually partway through "previously processed files" (the first occurrence)...

You can add parens. In this case, factoring out the common subexpression 
and replaced the repeated long name does the job.
                     p = os.path.join(root, file)
                     ppf = previously_processed_files
                     if p not in ppf and p[:-3] not in ppf:

[snip previous post]

-- 
Terry Jan Reedy




More information about the Python-list mailing list