[Python-Dev] Bare except clauses in PEP 348

"Martin v. Löwis" martin at v.loewis.de
Wed Aug 24 18:38:17 CEST 2005


Niko Matsakis wrote:
> Couldn't you just do:
> 
> txn = new_transaction ()
> try:
>      complete = 0
>      txn.begin ()
>      rtn = do_work ()
>      complete = 1
> finally:
>      if not complete: txn.abort ()
>      else: txn.commit ()
> 
> and then not need new builtins or anything fancy?

I personally dislike recording the execution path in
local variables. This is like setting a flag in a loop
before the break, and testing the flag afterwards.
You can do this, but the else: clause of the loop is
just more readable.

This specific fragment has also the bug that a
KeyboardInterrupt before the assignment to complete
will cause a NameError/UnboundLocalError; this
can easily be fixed by moving the assignment before
the try block.

Regards,
Martin


More information about the Python-Dev mailing list