nested-scopes redux

Aahz Maruch aahz at panix.com
Mon Sep 17 00:58:09 EDT 2001


In article <mailman.1000335016.31172.python-list at python.org>,
Cliff Wells  <logiplexsoftware at earthlink.net> wrote:
>
>Sorry to bring this subject back up again, but I just noticed a somewhat 
>annoying feature of the current nested-scope implementation.  One of the big 
>pluses of nested scopes is doing away with default arguments in lambda 
>functions.  Unfortunately the following bits of code behave differently:
>
>foo = []
>for i in [1, 2]:
>    foo.append(lambda i = i: i)
>
>and 
>
>from __future__ import nested_scopes
>foo = []
>for i in [1, 2]:
>    foo.append(lambda: i)
>
>In the first case the output from foo[0]() and foo[1]() is 1 and 2, 
>respectively - and it's what one would probably want.  In the second case, 
>the output is always 2.  This may be the expected behavior, but it kind of 
>does away with the benefits to providing a cleaner lambda call.

Because I'm not qualified to answer this, I posted this to python-dev,
and Guido sent this response:

    Cliff expects variable references to be bound to the value.  That's
    not how it works -- the idea of nested scopes is that the owner of the
    variable (the surrounding scope) can change the variable's value, and
    the user (the inner scope) will pick up the changes.  This is useful
    e.g. when an outer function calls an inner function repeatedly.  This
    is how nested scopes work in other languages.

    If Cliff wants to create N different lambdas with N different values
    for i, he'll have to do it the old way.

    Can you post this reply?

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

Tim Peters noted somewhat acerbically that I'd ignored a long thread
from last week that covered this issue in great detail, which is
probably why your post didn't get a response.
-- 
                      --- Aahz  <*>  (Copyright 2001 by aahz at pobox.com)

Hugs and backrubs -- I break Rule 6                 http://www.rahul.net/aahz/
Androgynous poly kinky vanilla queer het Pythonista   

We must not let the evil of a few trample the freedoms of the many.



More information about the Python-list mailing list