[Python-Dev] Explicit Lexical Scoping (pre-PEP?)

Guido van Rossum guido at python.org
Wed Jul 5 00:18:13 CEST 2006


On 7/4/06, Talin <talin at acm.org> wrote:
> Guido van Rossum wrote:
> > Also please explain what problem you are solving before proposing a
> > solution.

> Actually, the "problem" I am trying to solve is the debate on the
> mailing list. That is, I listen to what people are asking for, and what
> disagreements they have, and then I try to provide a solution that
> resolves the debate.
>
> In this case, there was a lot of discussion about lexical scoping, and
> various people proposing solutions (such as redefining the behavior of
> 'global') that I thought were (a) problematic, and (b) not a lot of bang
> for the buck (i.e. the disruption vs. utility tradeoff was poor IMHO.)
>
> To be honest, I really have no stake in this proposal, and I don't
> intend to spend any time defending it other than to correct
> misperceptions - however, I offer it as a potential starting point for
> people who are interested in the whole lexical scoping issue. If someone
> feels that this proposal gives them what they want, then great -
> otherwise I'll drop it.

Thanks; I appreciate the attempt. I just think that we're not quite
ready for more proposals (and certainly not for radical ones).
Instead, I'd like to go back to review the needs and desires first.

I think the needs are actually pretty simple. Python currently doesn't
allow assignment to variables in an outer non-global scope, and people
have shown by their behavior that they cannot get used to this
(otherwise the debate would have fizzled by now).

There are two fundamentally different mechanisms seen in programming
languages to control the binding of such variables. The most common
approach is to require declaration of variables in the scope to which
they belong. But Python doesn't do this, and I think it would be a
shame if we had to start doing this now -- the objections against your
proposal clearly show the problems if we try to mix this with Python's
traditional "assignment is declaration" philosophy. The other approach
is an extension of what Python already does for variables in the
global scope. ABC did this too (the SHARE command, see
http://homepages.cwi.nl/~steven/abc/qr.html#HOWTOs).

I think we have to continue to search for a solution that extends the
idea of global declarations.

I've proposed extending its meaning to refer to the nearest outer
scope where the variable is set; if there is no such scope it's an
error. This will break a small number of program but probably not very
many; still, it'll require a future statement or waiting until Python
3.0. The downside is that "global" is not a very intuitive word for
this new meaning. (Maybe ABC's SHARE would have been better.) We could
use a different keyword instead, e.g. 'outer'. I believe I've also
seen proposals in the past that used a number to indicate how many
scopes to go out; I don't like that at all.

I don't see anything else that's attractive. The realistic options are:

1. do nothing
2. extend global's meaning
3. add outer keyword

Personally I think I'd vote for (2) since it doesn't require a new
keyword. But that's only a slight preference over the other two.

Personally it's not a burning need; by the time you start feeling the
need to modify variables in an outer scope you should probably
consider refactoring using an explicit class to hold the state. But I
used the same argument to keep the current form of nested scopes (can
we say "closures"? But what exactly is the closure?) out of the door
and I lost that argument.

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


More information about the Python-Dev mailing list