[Python-Dev] PEP 340: Else clause for block statements

Jp Calderone exarkun at divmod.com
Mon May 2 16:02:49 CEST 2005


On Mon, 02 May 2005 07:46:31 -0600, Shane Hathaway <shane at hathawaymix.org> wrote:
>Anders J. Munch wrote:
>>     in opening('file1') as f1:
>>         ...
>>         in opening('file2') as f2:
>>             ...
>>     except IOError:
>>         print "file1 not available, I'll try again later"
>>
>> I rather like this version, because it is patently clear what should
>> happen if there is no except-clause: The exception propagates
>> normally.
>
>My eyes would expect the exception handler to also catch IOErrors
>generated inside the block statement body.  My eyes would be deceiving
>me, of course, but Python isn't currently so subtle and it probably
>shouldn't be.
>
>You could also do this with a suitable iterator.
>
>    def opening_or_skipping(fn):
>        try:
>            f = open(fn)
>        except IOError:
>            print "file1 not available, I'll try again later"
>        else:
>            try:
>                yield f
>            finally:
>                f.close()

  I don't think this version is really of much use.  It requires that you implement a different iterator for each kind of error handling you want to do.  Avoiding multiple different implementations is supposed to be one of the main selling points of this feature.

  Jp


More information about the Python-Dev mailing list