[Python-ideas] Except block for with-statement

Matt Joiner anacrolix at gmail.com
Sun Nov 20 03:43:05 CET 2011


Oops, just read the rest of the proposal which said exactly this.
On Nov 20, 2011 1:40 PM, "Matt Joiner" <anacrolix at gmail.com> wrote:

> That's a fantastic idea. As it stands context objects need to be done
> manually if exception handling is desired during enter. The except here
> could apply to the with statement only, since it's not possible to
> differentiate at present. Either way +1 to some consideration of Carl's
> proposal.
> On Nov 20, 2011 10:43 AM, "Carl M. Johnson" <
> cmjohnson.mailinglist at gmail.com> wrote:
>
>> I was looking through the "What's New for Python 3.3" file and I saw this
>> example code:
>>
>> try:
>>    with open("document.txt") as f:
>>        content = f.read()
>> except FileNotFoundError:
>>    print("document.txt file is missing")
>> except PermissionError:
>>    print("You are not allowed to read document.txt")
>>
>> and I thought it would be more elegant if you could drop the try.
>>
>> with open("document.txt") as f:
>>    content = f.read()
>> except FileNotFoundError:
>>    print("document.txt file is missing")
>> except PermissionError:
>>    print("You are not allowed to read document.txt")
>>
>> I assume that this has already been proposed and rejected. Does anyone
>> have a good explanation of why? ISTM that the with-statement is mostly just
>> a way of abstracting out try-except blocks, so it might be nice to have a
>> way of handling errors that pop up during the initialization phase instead
>> of just the code-block phase.
>>
>> I guess one obvious counter argument is that naive programmers might
>> think that the "except" applies to things in the block instead of just
>> things in the initializer. But naive programmers think lots of wrong
>> things. ;-)
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> http://mail.python.org/mailman/listinfo/python-ideas
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20111120/e7d71cad/attachment.html>


More information about the Python-ideas mailing list