[Python-Dev] Using and binding relative names (was Re: PEP forBetter Control of Nested Lexical Scopes)
Almann T. Goo
almann.goo at gmail.com
Sun Feb 26 21:18:12 CET 2006
> Would this apply to reading intermediate scopes without the global keyword?
Using a name from an enclosing scope without re-binding to it would
not require the "global" keyword. This actually is the case today
with "global" and accessing a name from the global scope versus
re-binding to it--this would make "global" more general than
explicitly overriding to the global scope.
> How would you know you aren't in inadvertently masking a name in a
> function you call?
I think is really an issue with the name binding semantics in Python.
There are benefits to not having variable declarations, but with
assignment meaning bind locally, you can already shadow a name in a
nested scope inadvertently today.
> In most cases it will probably break something in an obvious way, but I
> suppose in some cases it won't be so obvious.
Having the "global" keyword semantics changed to be "lexically global"
would break in the cases that "global" is used on a name within a
nested scope that has an enclosing scope with the same name. I would
suppose that actual instances in real code of this would be rare.
Consider:
>>> x = 1
>>> def f() :
... x = 2
... def inner() :
... global x
... print x
... inner()
...
>>> f()
1
Under the proposed rules:
>>> f()
2
PEP 227 also had backwards incompatibilities that were similar and I
suggest handling them the same way by issuing a warning in these cases
when the new semantics are not being used (i.e. no "from __future__").
-Almann
--
Almann T. Goo
almann.goo at gmail.com
More information about the Python-Dev
mailing list