[Python-Dev] Re: Dynamic nested scopes

M.-A. Lemburg mal@lemburg.com
Thu, 02 Nov 2000 17:05:14 +0100

Moshe Zadka wrote:
> On Wed, 1 Nov 2000, Guido van Rossum wrote:
> > [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.

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.

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 example uses attribute lookup -- the same can be done for
other nested objects by explicitely specifying the nesting

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).

Marc-Andre Lemburg
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/