variable scope in try ... EXCEPT block.

codewizard at gmail.com codewizard at gmail.com
Thu Jul 12 18:10:41 EDT 2018


On Thursday, July 12, 2018 at 5:45:52 AM UTC-4, Ben Bacarisse wrote:
> aleiphoenix writes:
> 
> [snip]
> 
>   When an exception has been assigned using as target, it is cleared at
>   the end of the except clause. This is as if
> 
>   except E as N:
>       foo
> 
>   was translated to
> 
>   except E as N:
>       try:
>           foo
>       finally:
>           del N

Is there a downside of implementing
it similarly to this (untested):

  # generate a unique name for __except_N
  except E as __except_N:
    if 'N' in locals() or N in globals():
      __original_N = N

    N = __except_N
    try:
      foo()
    finally:
      del __except_N

      if '__original_N' in locals():
        N = __original_N
        del __original_N
      else:
        del N


Regards,
Igor.



More information about the Python-list mailing list