Python and PEP8 - Recommendations on breaking up long lines?
Tim Chase
python.list at tim.thechases.com
Thu Nov 28 09:04:13 EST 2013
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? :-)
You could work around this with:
from functools import partial
in_ = partial(open, self.full_path)
out_ = partial(open, self.output_csv, 'ab')
with in_() as input, out_() as output:
do_stuff()
There's still room for programmer error if you don't have an
"output_csv" property on self and you get an AttributeError, but
that's more of a "dumb programmer error" rather than an actual
runtime exception.
-tkc
More information about the Python-list
mailing list