Python and PEP8 - Recommendations on breaking up long lines?

Ned Batchelder ned at nedbatchelder.com
Thu Nov 28 12:26:11 EST 2013


On 11/28/13 9:04 AM, Tim Chase wrote:
> On 2013-11-28 03:58, Steven D'Aprano wrote:
>>>       input = open(self.full_path)
>>>       output = open(self.output_csv, 'ab')
>>>       with input as input, output as output:
>>>           ...
>>
>> That's really clever! Why didn't I think of that?
>
> Because if the 2nd output fails, the input doesn't get closed as
> it {w,sh}ould in a with-statement? :-)

D'oh, sorry.  A doubly-nested with statement?

     input = open(self.full_path)
     with input:
         output = open(self.output_csv, 'ab')
         with output:
             ...

Of course, if you were willing to use a doubly-nested with statement, 
then the original open-in-the-with form would have fit in 80 chars... 
Also, I have a sneaking suspicion there's still an error case not 
handled well, someone will point it out if so!

--Ned.

>
> -tkc





More information about the Python-list mailing list