[Python-Dev] replacing 'global'
Neal Norwitz
neal at metaslash.com
Mon Oct 27 11:12:02 EST 2003
On Mon, Oct 27, 2003 at 07:11:16AM -0800, Guido van Rossum wrote:
>
> Hah. Another argument *against* rebinding by :=, and *for* a nonlocal
> declaration. With 'nonlocal n, m' in increment() (or however it's
> spelled :-) the intent is clear.
I dislike := very much. I think it will confuse newbies and thus be
abused. While I dislike the global declaration, I don't feel strongly
about changing or removing it.
The best alternative I've seen that addresses nested scope and the global
declaration. Is to borrow :: from C++:
foo = DEFAULT_VALUES
counter = 0
def reset_foo():
::foo = DEFAULT_VALUES
def inc_counter():
::counter += 1
def outer():
counter = 5
def inner():
::counter += outer::counter # increment global from outer
outer::counter += 2 # increment outer counter
The reasons why I like this approach:
* each variable reference can be explicit when necessary
* no separate declaration
* concise, no wording issues like global
* similarity between global and nested scopes
(ie, ::foo is global, scope::foo is some outer scope)
both the global and nested issues are handled at once
* doesn't prevent augmented assignment
* it reads well to me and the semantics are pretty clear
(although that's highly subjective)
Neal
More information about the Python-Dev
mailing list