Closures in python
Anton Vredegoor
anton at vredegoor.doge.nl
Thu Sep 18 09:05:18 EDT 2003
Hallvard B Furuseth <h.b.furuseth(nospam)@usit.uio(nospam).no> wrote:
>'closure' is a much-abused word.
>This is a closure over foo's x variable:
How about namespaces?
>def foo():
> x = 3
> def bar():
> x += 1
> return x
> return bar
>
>f = foo()
>print f() # print 4
>g = foo()
>print f() # print 5
>print g() # print 4
>
>...or it would be, if it worked.
This works:
def foo():
class ns:pass
ns.x = 3
def bar():
ns.x += 1
return ns.x
return bar
Anton
More information about the Python-list
mailing list