[Python-Dev] closure semantics

Alex Martelli aleaxit at yahoo.com
Tue Oct 21 19:58:21 EDT 2003

On Wednesday 22 October 2003 01:40, Guido van Rossum wrote:
> Eek.  Global statement inside flow control should be deprecated, not
> abused to show that global is evil. :-)

OK, let's (deprecate them), shall we...?

> > Plus. EVERY newbie makes the mistake of taking "global" to mean
> > "for ALL modules" rather than "for THIS module",
> Only if they've been exposed to languages that have such globals.

Actually, I've seen that happen to complete newbies too.  "global" is
a VERY strong word -- or at least perceived as such.

> > uselessly using global in toplevel,
> Which the parser should reject.

Again: can we do that in 2.4?

> I think it's not unreasonable to want to replace global with attribute
> assignment of *something*.  I don't think that "something" should have
> to be imported before you can use it; I don't even think it deserves
> to have leading and trailing double underscores.

Using attribute assignment is my main drive here.  I was doing it
via import only to be able to experiment with that in today's Python;-).

> Walter suggested 'global.x = 23' which looks reasonable; unfortunately
> my parser can't do this without removing the existing global statement
> from the Grammar: after seeing the token 'global' it must be able to
> make a decision about whether to expand this to a global statement or
> an assignment without peeking ahead, and that's impossible.

So it can't be global, as it must stay a keyword for backwards compatibility
at least until 3.0.  What about:
    sys.modules[__name__]  [[hmmm this DOES work today, but...;-)]]

> If we removed global from the language, how would you spell assignment
> to a variable in an outer function scope?  Remember, you can *not* use
> 'outer.x' because that already refers to a function attribute.

scope(outer).x , making 'scope' a suitable built-in factory function.  I do
think this deserves a built-in.

If we have this, maybe scope could also be reused as e.g.
scope(global).x = 23
?  I think the reserved keyword 'global' SHOULD give the parser no
problem in this one specific use (but, I'm guessing...!).


More information about the Python-Dev mailing list