[Python-ideas] Tweaking closures and lexical scoping to include the function being defined

Mike Meyer mwm at mired.org
Mon Sep 26 17:45:38 CEST 2011

When this conversation started discussing namespaces, it occurred to me that
we've had a number of suggestions for "statement-local" namespaces shot
down. It seems that they'd solve this case as well as the intended case. I
don't expect this to be acceptable, but since it solves part of this problem
as well as dealing with the issues for which it was originally created, I
thought I'd point it out.

I'm talking about the requests that we add the let/where statement found in
functional languages. Both add one new keyword. The syntax is:



statement where:

which creates a namespace containing names bound in "assignments" for the
duration of statement (or statements, as the case may be). I'm going to go
with the let version, because it's not clear how what the syntax should be
for where on a def statement.

The original examples were things like:

    sumsq = sum(a * a for a in mylist)
    value =  sumsq * 3 - sumsq

So you can deal with the case of wanting to preserve values during a binding
with something like:

res = []
for i in range(20):
        i = i
        res.append(lambda x: x + i)

Allowing a def in the statements means you can write the counter example
with something like:

    counter = 0
    def inc(change=1):
        nonlocal counter
        counter += change
        return counter

We have to declare counter nonlocal in order to rebind it in this case.

The odd thing here is that bindings in statements happen in the namespace
that the let occurs in, but lookups of nonlocal variables include the
namespace created by the let.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110926/b3493a29/attachment.html>

More information about the Python-ideas mailing list