[Python-ideas] Sublocal scoping at its simplest

Chris Angelico rosuav at gmail.com
Sun Apr 29 07:24:40 EDT 2018


On Sun, Apr 29, 2018 at 6:03 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> The challenge with doing this implicitly is that there's no indication
> whatsoever that the two "e"'s are different, especially given the
> longstanding precedent that the try/except level one will overwrite any
> existing reference in the local namespace.

My intention is that the "except" statement IS the indication that
they're different. Now that the name gets unbound at the exit of the
clause, the only indication that it overwrites is that, after "except
Exception as e:", any previous e has been disposed of. I'd hardly call
that a feature. Can you show me code that actually DEPENDS on this
behaviour?

> By contrast, if the sublocal marker could be put on the *name itself*, then:
>
> 1. Sublocal names are kept clearly distinct from ordinary names
> 2. Appropriate sublocal semantics can be defined for any name binding
> operation, not just exception handlers
> 3. When looking up a sublocal for code compiled in exec or eval mode,
> missing names can be identified and reported at compile time (just as they
> can be for nonlocal declarations) (Such a check likely wouldn't be possible
> for code compiled in "single" mode, although working out a suitable
> relationship between sublocal scoping and the interactive prompt is likely
> to prove tricky no matter what)

I'm aware of this, but that gets us right back to debating syntax, and
I'm pretty sure death and syntaxes are the two things that we can
truly debate forever. :)

ChrisA


More information about the Python-ideas mailing list