What are python closures realy like?

Karl Kofnarson kofnarson at gmail.com
Fri Dec 1 16:33:44 EST 2006


Hi,
while writing my last program I came upon the problem
of accessing a common local variable by a bunch of 
functions.
I wanted to have a function which would, depending on
some argument, return other functions all having access to
the same variable. An OO approach would do but why not
try out closures...
So here is a simplified example of the idea:
def fun_basket(f):
    common_var = [0]
    def f1():
        print common_var[0]
        common_var[0]=1
    def f2():
        print common_var[0]
        common_var[0]=2
    if f == 1:
        return f1
    if f == 2:
        return f2
If you call f1 and f2 from the inside of fun_basket, they
behave as expected, so common_var[0] is modified by
whatever function operates on it.
However, calling f1=fun_basket(1); f2 = fun_basket(2) and
then f1(); f2() returns 0 and 0. It is not the way one would
expect closures to work, knowing e.g. Lisp make-counter.
Any ideas what's going on behind the scene?



More information about the Python-list mailing list