[Python-ideas] try-else without except or finally

Paul Moore p.f.moore at gmail.com
Thu Nov 10 21:13:31 CET 2011


On 10 November 2011 19:55, Terry Reedy <tjreedy at udel.edu> wrote:
> On 11/10/2011 8:28 AM, Steven D'Aprano wrote:
>
>> try:
>> ...
>> else:
>> # Run the else block unless we exit the try block with an exception.
>> ...
>
> As Dennis Kaarsemaker already pointed out in his response to the OP, the
> 'else:' is not needed and this is the same (unless we are missing something)
> as
>
> try:
>  ...
>  ...
>
> In other words, *every* statement in a try block after the first is preceded
> by an implicit 'run this unless we already exited with an exception'.

Ah! I noted the post by Dennis Kaarsemaker pointing out that the
original code could be rewritten without needing the dummy except or
the else, but hadn't spotted that the argument generalised to *all*
uses of an else without an except.

Paul.



More information about the Python-ideas mailing list