[Python-Dev] PEP for Better Control of Nested Lexical Scopes

Josiah Carlson jcarlson at uci.edu
Tue Feb 21 20:31:50 CET 2006

Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Josiah Carlson wrote:
> > Mechanisms which rely on manipulating variables within closures or
> > nested scopes to function properly can be elegant, but I've not yet seen
> > one that *really* is.
> It seems a bit inconsistent to say on the one hand
> that direct assignment to a name in an outer scope
> is not sufficiently useful to be worth supporting,
> while at the same time providing a way to do it for
> one particular scope, i.e. 'global'. Would you
> advocate doing away with it?

I didn't conceive of the idea or implementation of 'global', it was
before my time.  I have found that *using* global can be convenient (and
sometimes even directly manipulating globals() can be even more
convenient).  However, I believe global was and is necessary for the
same reasons for globals in any other language.

Are accessors for lexically nested scopes necessary?  Obviously no.  The
arguments for their inclusion are: easier access to parent scopes and
potentially faster execution.

The question which still remains in my mind, which I previously asked,
is whether the use cases are compelling enough to warrant the feature

>  > Of course using
> > classes directly with a bit of work can offer you everything you want
> > from a closure, with all of the explcitness that you could ever want.
> There are cases where the overhead (in terms of amount
> of code) of defining a class and creating an instance of
> it swamps the code which does the actual work, and,
> I feel, actually obscures what is being done rather
> than clarifies it. These cases benefit from the ability
> to refer to names in enclosing scopes, and I believe
> they would benefit further from the ability to assign
> to such names.

    class namespace: pass

    def fcn(...):
        foo = namespace()

Overwhelms the user?

> Certainly the feature could be abused, as can the
> existing nested scope facilities, or any other language
> feature for that matter. Mere potential for abuse is
> not sufficient reason to reject a feature, or the
> language would have no features at all.

Indeed, but as I have asked, I would like to see some potential
nontrivial *uses*. No one has responded to this particular request. When
I am confronted with a lack of uses, and the potential for abuses, I'm
going to have to side on "no thanks, the potential abuse outweighs the
nonexistant nontrivial use".

> Another consideration is efficiency. CPython currently
> implements access to local variables (both in the
> current scope and all outer ones except the module
> scope) in an extremely efficient way. There's
> always the worry that using attribute access in
> place of local variable access is greatly increasing
> the runtime overhead for no corresponding benefit.

Indeed, the only benefit to using classes is that you gain explicitness. 
To gain speed in current Python, one may need to do a bit more work
(slots, call frame hacking, perhaps an AST manipulation with the new AST
branch, etc.).

> You mention the idea of namespaces. Maybe an answer
> is to provide some lightweight way of defining a
> temporary, singe-use namespace for use within
> nested scopes -- lightweight in terms of both code
> volume and runtime overhead. Perhaps something like
>    def my_func():
>      namespace foo
>      foo.x = 42
>      def inc_x():
>        foo.x += 1

Because this discussion is not about "how do I create a counter in
Python", let's see some examples which are not counters and which are
improved through the use of this "namespace", or "use", "scope", etc.

> > Introducing these two new keywords is equivalent to
> > encouraging nested scope use.  Right now nested scope
> > use is "limited" or "fraught with gotchas".
> What you seem to be saying here is: Nested scope use
> is Inherently Bad. Therefore we will keep them Limited
> and Fraught With Gotchas, so people will be discouraged
> from using them.
> Sounds a bit like the attitude of certain religious
> groups to condoms. (Might encourage people to have
> sex -- can't have that -- look at all the nasty diseases
> you can get!)

If you take that statement within the context of the other things I had
been saying in regards to closures and nested scopes, namely that I find
their use rarely, if ever, truely elegant, it becomes less like "condom
use" as purported by some organizations, and more like kicking a puppy
for barking: it is of my opinion that there are usually better ways of
dealing with the problem (don't kick puppies for barking and don't use

 - Josiah

More information about the Python-Dev mailing list