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

Guido van Rossum guido at python.org
Mon Jul 10 05:39:01 CEST 2006


On 7/9/06, Talin <talin at acm.org> wrote:
> Talin wrote:
> > Some alternatives:
> >
> >      use x
> >      using x
> >      with x     -- recycle a keyword?
> >      reuse x
> >      use extant x
> >      share x
> >      common x
> >      same x
> >      borrow x
> >      existing x

Of these, I like reuse, share, common slightly better than the rest.

> > Although, to be perfectly honest, the longer this discussion goes on,
> > the more that I find that I'm not buying Guido's argument about it being
> > better to define this at the point of use rather than at the point of
> > definition. I agree with him that "point of use" is more Pythonic, but
> > I'm also beginning to believe that there are some good reasons why many
> > other languages do it the other way.

Well, I still don't like point of definition. It means that something
far away can change the interpretation of something local. The reason
other languages do it differently is that variable declarations are
obligatory. It's really unacceptable that when I see

 def foo():
   x = 12

I would have to search the entire module for a possible definition of
a global named 'x' to see whether the x assigned to here is local or
not.

> > Part of the reason why its so hard to name this feature is that it's
> > real name is something like "Hey, Python, you know that cool funky thing
> > you do with defining variables in the same scope as they are assigned?
> > Well, don't do that here."

Sorry, that sounds like a B.S. argument. You can translate most
language constructs into long sentences if you want to.

> (Followup to my own comment)
>
> There are really 3 places where you can indicate that a variable is to
> be reused instead of redefined: 1) The point of definition in the outer
> scope, 2) A declaration in the inner scope, and 3) The actual point of
> assignment.
>
> #1 is what I've been pushing for, #2 is what most of the discussion has
> been about, #3 has been talked about a little bit in the context of an
> augmented assignment operator.
>
> I actually like #3 a little better than #2, but not with a new operator.
> I'm thinking more along the lines of a keyword that modifies and
> assignment statement:
>
>     rebind x = 10
>
> Other possible keywords are: modify, mutate, change, update, change, etc...

A problem with this is the ambiguity if some assignments to x use the
rebind keyword and others don't. Then there's a local x and also a
nonlocal x. When x is used in an expression (even on the RHS of a
rebind statement!) it would be interpreted as the local one.

> My gut feeling is that most code that wants to use this feature only
> wants to use it in a few places. A good example is fcgi.py (implements
> WSGI for FastCGI), where they use a mutable array to store a flag
> indicating whether or not the headers have already been sent.

Then let's allow

  nonlocal x = 12

as a shortcut for

  nonlocal x
  x = 12

Bash users should be familiar with this from e.g. export FOO=bar.

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


More information about the Python-Dev mailing list