[Python-Dev] statically nested scopes

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


[MAL]
> > > It may not look serious, but changing the Python lookup scheme
> > > is, since many inspection tools rely and reimplement exactly
> > > that scheme. With nested scopes, there would be next to no
> > > way to emulate the lookups using these tools.

[GvR]
> > So fix the tools.

[MAL]
> Eek. Are you proposing to break all the Python IDE that are
> just appearing out there ?

Yes.  If a tool does variable scope analysis, it should be prepared
for changes in the rules.  Otherwise we might as well have refused the
syntax changes in 2.0 because they required changes to tools!

> > > To be honest, I don't think static nested scopes buy us all that
> > > much. You can do the same now, by using keyword arguments which
> > > isn't all that nice, but works great and makes the scope clearly
> > > visible.
> > 
> > Yes.  It's a hack that gets employed over and over.  And it has
> > certain problems.  We added 'import as' to get rid of a common
> > practice that was perceived unclean.  Maybe we should support nested
> > scopes to get rid of another unclean common practice?
> 
> I think the common practice mainly comes from the fact,
> that by making globals locals which can benefit from LOAD_FAST
> you get a noticable performance boost.
> 
> So the "right" solution to these weird looking hacks would
> be to come up with a smart way by which the Python compiler
> itself can do the localizing.

Can you elaborate?  I dun't understand what you are proposing here.

> Nested scopes won't help eliminating the current keyword 
> practice.

Why not?  I'd say that

    def create_adder(n):
        def adder(x, n=n): return x+n
	return adder

is a hack and that nested scopes can fix this by allowing you to write


    def create_adder(n):
        def adder(x): return x+n
	return adder
    
like one would expect.  (Don't tell me that it isn't a FAQ why this
doesn't work!)

> > I'm not saying that we definitely should add this to 2.1 (there's
> > enough on our plate already) but we should at least consider it, and
> > now that we have cycle GC, the major argument against it (that it
> > causes cycles) is gone...
> 
> Hmm, so far the only argument for changing Python lookups
> was to allow writing lambdas without keyword hacks. Does this
> really warrant breaking code ?
> 
> What other advantages would statically nested scopes have ?

Doing what's proper.  Nested scopes are not a bad idea.  They weren't
implemented because they were hard to get right (perhaps impossible
without creating cycles), and I was okay with that because I didn't
like them; but I've been convinced that examples like the second
create_adder() above should reall work.  Just like float+int works
(in Python 0.1, this was a type-error -- you had to case the int arg
to a float to get a float result).

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