[Python-Dev] Tangent on class level scoping rules (was Re: PEP 463: Exception-catching expressions)
rosuav at gmail.com
Sat Feb 22 10:45:30 CET 2014
On Sat, Feb 22, 2014 at 10:57 AM, Greg Ewing
<greg.ewing at canterbury.ac.nz> wrote:
> Nick Coghlan wrote:
>> As Chris later noted, you likely *could* still implement expression
>> local name binding for an except expression without a full closure, it
>> would just be rather difficult.
> I'm still not convinced it would be all *that* difficult.
> Seems to me it would be semantically equivalent to
> renaming the inner variable and adding a finally clause
> to unbind it. Is there something I'm missing?
An inner scope should shadow rather than unbinding. Ideally:
spam = "Initial spam"
except Exception as spam:
assert isinstance(spam, Exception)
assert [spam for spam in ["List Comp"]] == "List Comp"
with open("test","w") as spam:
assert (lambda spam: spam)("Function") == "Function"
assert spam == "Initial spam"
Currently, the list comp and lambda work that way. The exception will
unbind the name, not changing the scope at all but preventing refloop
after exit. The 'with... as' works in the same scope and leaves the
name bound, which is a bit surprising in some cases (you usually end
up with a closed file object, or whatever it be, lurking around).
A clean inner-scope concept would solve all of this. The lambda would
still be a closure, because you can pass that around. All the others
would be inner scopes - shadowing cleanly and then disappearing.
More information about the Python-Dev