[Python-Dev] nested-scopes redux (fwd)

Guido van Rossum guido@python.org
Mon, 17 Sep 2001 00:32:13 -0400


> Haven't seen any response to this, and I think it deserves one (then
> again, I could be wrong):

Alas, few people here have time to scan comp.lang.python.

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/)

> ------- start of forwarded message -------
> From: Cliff Wells <logiplexsoftware@earthlink.net>
> Newsgroups: comp.lang.python
> Subject: nested-scopes redux
> Date: Wed, 12 Sep 2001 15:45:17 -0700
> Message-ID: <mailman.1000335016.31172.python-list@python.org>
> Reply-To: logiplexsoftware@earthlink.net
> To: python-list@python.org
> 
> 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.
> 
> -- 
> Cliff Wells
> Software Engineer
> Logiplex Corporation (www.logiplex.net)
> (503) 978-6726 x308
> (800) 735-0555 x308
> 
> ------- end of forwarded message -------
> 
> 
> _______________________________________________
> Python-Dev mailing list
> Python-Dev@python.org
> http://mail.python.org/mailman/listinfo/python-dev