[Python-ideas] with statement syntax forces ugly line breaks? (original) (raw)
Michael Foord fuzzyman at voidspace.org.uk
Thu Sep 9 16:34:25 CEST 2010
- Previous message: [Python-ideas] with statement syntax forces ugly line breaks?
- Next message: [Python-ideas] Why not f(*my_list, *my_other_list) ?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 9 September 2010 14:53, M.-A. Lemburg <mal at egenix.com> wrote:
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.
If you need to distinguish at a higher level then you have no choice. I was really just pointing out that there are semantic differences as well, and in fact the code you posted is less safe than the one line version. You lose some of the error handling built-in to context manager creation.
Michael
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 <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/b6207779/attachment.html>
- Previous message: [Python-ideas] with statement syntax forces ugly line breaks?
- Next message: [Python-ideas] Why not f(*my_list, *my_other_list) ?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]