Re: Scoping (was Re: Lambda binding solved?)

Guido.van.Rossum@cwi.nl
Fri, 04 Mar 1994 23:51:50 +0100

> I note that, since local name spaces in modules (and scripts and
> interactive commands) are the same as the globals, one *could* have
> the global namespace of a class and function definitions be the local
> namespace of the enclosing code block. The 'global' declaration would
> serve to both access and modify truly global bindings:
>
> current = [3, 4]
> class PointInPage:
> margin = computeMargin(); # At class definition time!
> def addXY(pt):
> global current
> return [pt[0]+current[0]+margin,pt[1]+current[1]]

Alas, this would break more code than you think: not only current, but
also PointInPage, any other classes and functions you might define at
the module level, and last-but-not-least any modules imported at the
start of the module would be unreachable without a global statement!
Unless you introduce CSNS (classic static nested scopes), but you
don't seem to popose that here... The *name* current is only read,
not assigned to in your example.

> For searching and modifying non-local, non-global contexts, I can't help
> wondering if Python shouldn't have the equivalent of PostScript's 'store'
> primitive. Perhaps a declaration:
>
> scoped foo # similar to the 'nonlocal' proposed before
> foo = 8
>
> would attempt to bind foo in the closest non-local namespace, if it found
> a binding for 'foo' (including the global namespace), else it would bind it
> in the local namespace, just as if the 'scoped' declaration wasn't there.
> It would be arguable whether 'scoped' be required to do searching through
> enclosing local namespaces, or whether such a search be done by default.

This is dangerously close to introducing CSNS. *If* you were to do
so, your proposed semantics of scoped seem allright. I still think
there is not enough need for CSNS to warrant this kind of construct,
which can easily confuse the human reader when there are more than two
or three scopes involved and some are textually far apart (as Tim or
Steven pointed out earlier).

> One could also argue whether 'global' is needed at all, perhaps being
> replaced by naming conventions like, "all variables defined in the outermost
> namespace should begin with a capital letter." 'Exec' would take a local
> namespace (dictionary) and a list of non-local namespaces.

Nah, I think conventions based upon such subtle indications as case
are both alien to Python *and* too late to introduce now...

> Then again, maybe my thinking has been warped from all the NeWS programming
> I've done -- in PostScript sometimes think I :-).

Well, human readability of PostScript was never a design criterion was
it? :-)

> As for the other topic I raised:
[discussion of metaclasses in NeWS deleted]

Hmmm... It may indeed be possible to change the semantics and
implementation of classes subtly enough to somehow allow delegation or
other tricks played with assigning to class attributes while still
speeding up the normal case. I suggest that anybody who uses classes
in a non-boilerplate manner and/or assigns to class attributes of all
but the most derived class be aware of potential changes in semantics!

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>
URL: <http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>