<div dir="ltr">Is that any more ambiguous than for...else?<div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Apr 16, 2015 at 2:22 AM, Andrew Barnert <span dir="ltr"><<a href="mailto:abarnert@yahoo.com.dmarc.invalid" target="_blank">abarnert@yahoo.com.dmarc.invalid</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Apr 15, 2015, at 23:39, Kale Kundert <<a href="mailto:kale@thekunderts.net">kale@thekunderts.net</a>> wrote:<br>
><br>
> Hello everyone,<br>
><br>
> I'd like to propose a little bit of syntactic sugar: allowing with-blocks to be<br>
> followed by except- and finally-blocks just like try-blocks. For example:<br>
><br>
> with open('spam.txt') as file:<br>
> print(file.read())<br>
> except IOError:<br>
> print('No spam here...')<br>
> finally:<br>
> print('done.')<br>
><br>
> This proposed syntax is semantically equivalent to wrapping a with-block within<br>
> a try-block like so:<br>
><br>
> try:<br>
> with open('spam.txt') as file:<br>
> print(file.read())<br>
> finally:<br>
> print('done.')<br>
> except IOError:<br>
> print('No spam here...')<br>
<br>
</span>Why does the finally come before the except? Also, it's still indented with the with, which means you seem to be defining with-except-finally in terms of with-finally, and not defining with-finally at all. Did you mean to have the finally after the except and dedented?<br>
<br>
Anyway, I like the idea, but does the grammar work? A with that has an optional except and finally at the same level seems potentially ambiguous. For example:<br>
<br>
with spam:<br>
pass<br>
with eggs:<br>
pass<br>
finally:<br>
pass<br>
<br>
If you think about it, it's pretty obvious that the finally goes with the second with, not the first. But can the parser tell that? Or a human who's scanning the code rather than thinking carefully about it? Or tools like auto-indenters?<br>
<br>
(I think this problem doesn't arise for try, because it must have at least except or finally.)<br>
<br>
It should be easy to edit the grammar and build the parser to at least the first part of the question; for the last part, trying to hack up a few different external tools like Emacs python-mode. But for the human question, I'm not sure how to answer it quite so easily...<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
><br>
> I see two advantages to the proposed syntax. First and most obviously, it saves<br>
> an extra line and an extra indentation level. One line may not be a big deal,<br>
> but one indentation level can really affect readability. Second and more<br>
> conceptually, it makes sense to think about exception handling in the context of<br>
> a with-block. More often than not, if you're using a with-block, you're<br>
> expecting that something in that block could throw an exception. Usually<br>
> with-blocks are used to make sure resources (e.g. files, database sessions,<br>
> mutexes, etc.) are properly closed before the exception is propogated. But I<br>
> very often want to do some custom clean-up as well (alert the user, etc.).<br>
> Currently that requires wrapping the whole thing in a try-block, but allowing<br>
> with-blocks to behave as try-blocks is a more direct way to express what is meant.<br>
><br>
> I was curious how often with-blocks are actually wrapped in try-blocks for no<br>
> other purpose than catching exceptions raised in the with-block. So I searched<br>
> through a number of open source projects looking (roughly) for that pattern:<br>
><br>
> Project with [1] try-with [2]<br>
> ============== ======== ============<br>
> django 230 17<br>
> ipython 541 8<br>
> matplotlib 112 3<br>
> moinmoin 10 0<br>
> numpy 166 1<br>
> pillow/pil 1 0<br>
> pypy 254 4<br>
> scipy 163 2<br>
> sqlalchemy 36 0<br>
> twisted 72 1<br>
> ============== ======== ============<br>
> total 1585 36 (2.27%)<br>
><br>
> [1]: grep -Po '^\s*with .*:' **/*.py<br>
> [2]: grep -Poz 'try:\s*with .*:' **/*.py<br>
><br>
> Assuming these projects are representative, about 2% of the with-blocks are<br>
> directly wrapped by try-blocks. That's not a huge proportion, but it clearly<br>
> shows that this pattern is being used "in the wild". Whether or not it's worth<br>
> changing the language for the benefit of 2% of with-blocks is something to<br>
> debate though.<br>
><br>
> What do people think of this idea?<br>
><br>
> -Kale Kundert<br>
><br>
> _______________________________________________<br>
> Python-ideas mailing list<br>
> <a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
> Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr">Ryan</div><div dir="ltr">[ERROR]: Your autotools build scripts are 200 lines longer than your program. Something’s wrong.<br></div><div dir="ltr"><div><a href="http://kirbyfan64.github.io/" target="_blank">http://kirbyfan64.github.io/</a><div style="display:inline-block;width:16px;height:16px"> </div></div></div></div></div></div></div></div>
</div>