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

Michael Foord fuzzyman at voidspace.org.uk
Thu Sep 9 15:41:52 CEST 2010


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.

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
>



-- 
http://www.voidspace.org.uk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100909/ce124b99/attachment.html>


More information about the Python-ideas mailing list