[Python-ideas] with-except-finally blocks

Sturla Molden sturla.molden at gmail.com
Sun Apr 19 13:28:32 CEST 2015


Steven D'Aprano <steve at pearwood.info> wrote:

> This has two obvious, and slightly different, interpretations:
> 
> with open('spam.txt') as file:
>     try:
>         print(file.read())
>     except IOError:
>         print('No spam here...')
>    finally:
>         print('done.')
> 
> 
> try:
>     with open('spam.txt') as file:
>         print(file.read())
> except IOError:
>     print('No spam here...')
> finally:
>     print('done.')


Sometimes I want the first, sometimes the second. And I expect a conscious
mind-reading interpreter which gives me the one I want. If the interpreter
in retrospect thinks it has given me the wrong one it should raise
SorryError.


Sturla



More information about the Python-ideas mailing list