Nested scopes and lexical closures
Paul Rubin
phr-n2001 at nightsong.com
Sat Aug 18 18:00:07 EDT 2001
I see that Python 2.1.1 lets you import nested scopes from the future.
However, I couldn't easily tell from the documentation whether that
means you can make lexical closures:
def counter():
value = 0
def incr():
value += 1
return value
return incr
This turns out to throw a runtime exception:
>>> a=counter()
>>> a()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/tmp/python-170562C", line 6, in incr
UnboundLocalError: local variable 'value' referenced before assignment
Of course that's disappointing and I'm wondering if there's some
intention to fix it in the future. Meanwhile I think the
documentation of nested scopes should be updated to make it clear that
closures don't work.
More information about the Python-list
mailing list