[Python-3000] Draft PEP for outer scopes
Delaney, Timothy (Tim)
tdelaney at avaya.com
Thu Nov 2 22:18:03 CET 2006
Steven Bethard wrote:
> On 11/1/06, Ka-Ping Yee <python at zesty.ca> wrote:
>> I think a fairer survey example would be something like this:
>>
>> n = 1
>> def f():
>> n = 2
>> def g():
>> global n = 99
>> return n
>> g()
>>
>> f()
>> print n
>>
>> Which 'n' do you expect g() to change?
>
> The only reason I didn't use that in the first place is that is seems
> extremely unlikely in real code. Who uses the same name for a
> module-level binding and a function-local binding?
>
> So yes, that's the corner case, but I contend that the corner case
> will almost never come up.
A much more likely corner case is (using existing syntax):
def f():
def g()
global n
n = 99
return n
g()
f()
print n
What does this do? It depends on if f() has a binding of "n". If it
does, the above throws a NameError. If it doesn't, it prints 99.
For "nonlocal", this is a non-issue - it's a syntax error. But if we
reuse "global", the nested function is context-dependent.
If "global" were to change in Py3K to require an existing binding at the
time the "global" keyword was executed, this would then have the
semantics of "nonlocal" and be context-independent again. So I think any
proposal to reuse "global" has to include these semantics.
Tim Delaney
More information about the Python-3000
mailing list