[Python-ideas] New scope for exception handlers

Chris Angelico rosuav at gmail.com
Sat Apr 9 01:26:26 EDT 2016

On Sat, Apr 9, 2016 at 7:03 AM, Joseph Jevnik <joejev at gmail.com> wrote:
> def g():
>     a = 1
>     try:
>         ...
>     except:
>         a = 2
>     return a
> I think this code is well behaved and should continue to work as it already
> does. There are a couple of ways to implment this new behavior but I think
> the simplest way to do this would be to treat the handler as a closure where
> all the free variables defined as nonlocal.

I'm not sure what the point there is; when do you need this kind of
thing, and why only in the 'except' clause?

Also, what if the name is assigned to in the 'try' and the 'except'
but nowhere else?

If you're curious, I actually put together a "just for the fun of it"
patch that adds a form of sub-function scoping to Python. It'd be a
great way to figure out just how far this flies in the face of
Python's design.


More information about the Python-ideas mailing list