[Python-Dev] Re: Dynamic nested scopes

Guido van Rossum guido@python.org
Thu, 02 Nov 2000 00:45:09 -0500

> > > [MAL]
> > > > Dynamic nested scopes is another topic... those are *very*
> > > > useful; especially when it comes to avoiding global variables
> > > > and implementing programs which work using control objects
> > > > instead of global function calls.
> > >
> > > Marc-Andre, what are Dynamic nested scopes?

> > If MAL means dynamic scoping (which I understood he does), then this
> > simply means:
> > 
> > when looking for a variable "foo", you first search for it in the local
> > namespace. If not there, the *caller's* namespace, and so on. In the
> > end, the caller is the __main__ module, and if not found there, it is
> > a NameError.

Ah, yuck.  For variable namespace, this is a really bad idea.  For
certain other things (e.g. try/except blocks, Jeremy's example) it is
of course OK.

> That would be one application, yes.
> With dynamic scoping I meant that the context of a lookup is
> defined at run-time and by explicitely or implicitely
> hooking together objects which then define the nesting.

But certainly you're not thinking of doing this to something as basic
to Python's semantics as local/global variable lookup!

> Environment acquisition is an example of such dynamic scoping:
> attribute lookups are passed on to the outer scope in case they
> don't resolve on the inner scope, e.g. say you have
> object a with a.x = 1; all other objects don't define .x.
> Then a.b.c.d.x will result in lookups
> 1. a.b.c.d.x
> 2. a.b.c.x
> 3. a.b.x
> 4. a.x -> 1

This seems only remotely related to dynamic scopes.  There are
namespaces here, but not "scopes" as I think of them: a scope defines
the validity for an unadorned variable.  Static scopes mean that this
is determined by the program source structure.  Dynamic scopes means
that that this is determined by run-time mechanisms.  Python uses
static scoping for local variables, but a dynamic scoping mechanism
for non-local variable references: first it does a module-global
lookup, then it looks for a built-in name.

When we're not talking about simple name lookup, we should speak of
namespaces (which can also have lifetimes).

> This example uses attribute lookup -- the same can be done for
> other nested objects by explicitely specifying the nesting
> relationship.
> Jim's ExtensionClasses allow the above by using a lot of
> wrappers around objects -- would be nice if we could come
> up with a more general scheme which then also works for
> explicit nesting relationships (e.g. dictionaries which
> get hooked together -- Jim's MultiMapping does this).

I think we're getting way off track here.

--Guido van Rossum (home page: http://www.python.org/~guido/)