[Python-Dev] 2.5 and beyond

Tim Peters tim.peters at gmail.com
Sat Jul 1 01:23:56 CEST 2006


[Andrew Koenig]
>>> I saw messages out of sequence and did not realize that this would be a
>>> change in behavior from 2.4.  Sigh.

[Ka-Ping Yee]
>> Yes, this is not a good time to change it.

>>> I hope Py3000 has lexical scoping a la Scheme...

>> Me too -- that would be really nice.

[Guido]
> That's not a very constructive proposal (especially since I don't know
> Scheme). Perhaps you could elaborate on what needs to change?

It's effectively the opposite of Python <0.1 wink>:  a name is local
to a scope in Scheme if and only if a declaration says it is.  For
example, the "let" family of forms is often used for this purpose, and

(let ((x 2) (y 3))    # declares x and y as local to this `let`, and
gives initial values
  (let ((x 7)
        (z (+ x y)))      # x comes from outer `let` so is 2, and z is 2+3=5
    (* z x)))        # x comes from inner `let`, so this is 5*7=35

If you use `let*` instead of `let` in the inner one, z picks up the
inner x=7, so that z is 7+3=10, and the result is 7*10 = 70 instead.
The bindings in a `let` "happen" in an undefined order.  In `let*`, a
binding is visible "to its right" within the `let*`.  Then there's
`letrec`, which allows establishing mutually recursive bindings.

While the `let` family is entirely about declaration, there are lots
of other forms that mix in some declaration as part of their purpose
(for example, the names in a lambda's argument list are local to the
lambda's body), but they're all explicit in Scheme.

I read "a la Scheme" here as "actually nothing like Scheme, except I
want a non-tricky way to rebind a name from an enclosing scope within
an enclosed scope".  In Scheme, the scope a name x belongs to is found
by searching enclosing scopes until you hit the first with an explicit
"x belongs to me" declaration (OK, there's a hokey fallback to "top
level" definitions too).  Searching "textually up" always suffices
(there's no form of delayed declaration -- a name must be declared
before use).  Scheme's assignment of assignment:

     (set! variable expression)

has nothing to do with establishing the scope of `variable`.


More information about the Python-Dev mailing list