[Python-3000] Draft PEP for outer scopes
Michael Urman
murman at gmail.com
Mon Nov 6 15:33:24 CET 2006
> def weird(local=True):
> if local:
> n = 1
> else:
> global n # [ or nonlocal n, in Nick's ]
> n += 1
> return n
I'm not sure what you're both trying to explain here. First off, the
above code yields a SyntaxWarning. Secondly all its accesses to n are
global accesses. The global keyword is currently a modifier for the
whole function. Thus the local parameter is misleading; it should be
called reset.
<stdin>:1: SyntaxWarning: name 'n' is assigned to before global declaration
>>> dis.dis(weird)
2 0 LOAD_FAST 0 (local)
3 JUMP_IF_FALSE 10 (to 16)
6 POP_TOP
3 7 LOAD_CONST 1 (1)
10 STORE_GLOBAL 1 (n)
13 JUMP_FORWARD 11 (to 27)
>> 16 POP_TOP
6 17 LOAD_GLOBAL 1 (n)
20 LOAD_CONST 1 (1)
23 INPLACE_ADD
24 STORE_GLOBAL 1 (n)
7 >> 27 LOAD_GLOBAL 1 (n)
30 RETURN_VALUE
Would you expect the overloaded global keyword to apply differently,
or just have slightly different scoping semantics? Would a nonlocal
keyword apply differently? Would we allow multiple function-global
parameter modifiers?
I personally expect that while there is a theoretical clash between
variable names in nested scopes, that's already a poor coding
decision. The module level globals should not unintentionally collide
with function-local non-local access. Thus reusing the global keyword
is not a practical limitation.
--
Michael Urman http://www.tortall.net/mu/blog
More information about the Python-3000
mailing list