[Python-ideas] with statement syntax forces ugly line breaks?

M.-A. Lemburg mal at egenix.com
Thu Sep 9 15:53:49 CEST 2010


Michael Foord wrote:
> On 9 September 2010 14:32, M.-A. Lemburg <mal at egenix.com> wrote:
> 
>> [snip...]
>> Why do you need to put everything on one line ?
>>
>> afh = open(args.actual, encoding="utf-8")
>> efh = open(args.expected, encoding="utf-8")
>>
>> with afh, efh:
>>   ...
>>
>> In the context of files, the only purpose of the with statement
>> is to close them when leaving the block.
>>
>>>>> a = open('/etc/passwd')
>>>>> b = open('/etc/group')
>>
> 
> If my understanding is correct (which is perhaps unlikely...), using a
> single line will close a if opening b fails. Whereas doing them separately
> before the with statement risks leaving the first un-exited if creating the
> second fails.

Right, but if you stuff everything on a single line, your
error handling will have a hard time figuring out which of
the two failed to open.

I was under the impression that Mark wanted to "protect" the
inner block of the with statement, not the context manager
creation itself.

As usual: hiding away too much stuff in your closet makes things
look tidy, but causes a hell of a mess if you ever need to open
it again :-)

> Michael
> 
> 
>>>>> with a,b: print a.readline(), b.readline()
>> ...
>> at:x:25:25:Batch jobs daemon:/var/spool/atjobs:/bin/bash
>> at:!:25:
>>
>>>>> a
>> <closed file '/etc/passwd', mode 'r' at 0x7f0093e62390>
>>>>> b
>> <closed file '/etc/group', mode 'r' at 0x7f0093e62420>
>>
>> --
>> Marc-Andre Lemburg
>> eGenix.com
>>
>> Professional Python Services directly from the Source  (#1, Sep 09 2010)
>>>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
>> ________________________________________________________________________
>> 2010-08-19: Released mxODBC 3.1.0              http://python.egenix.com/
>> 2010-09-15 <http://python.egenix.com/%0A2010-09-15>: DZUG Tagung, Dresden,
>> Germany                   6 days to go
>>
>> ::: Try our new mxODBC.Connect Python Database Interface for free ! ::::
>>
>>
>>   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
>>    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
>>           Registered at Amtsgericht Duesseldorf: HRB 46611
>>               http://www.egenix.com/company/contact/
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> http://mail.python.org/mailman/listinfo/python-ideas
>>
> 
> 
> 

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Sep 09 2010)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________
2010-08-19: Released mxODBC 3.1.0              http://python.egenix.com/
2010-09-15: DZUG Tagung, Dresden, Germany                   6 days to go

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/



More information about the Python-ideas mailing list