[lambda]Is the behavior expected?

Alphones Xiaobin.Huang at gmail.com
Wed Nov 26 07:33:21 EST 2008


Hi all,


def getFunc(x):
    return lambda y : x + y

if __name__ == '__main__':
    todo = []
    proc = getFunc(1)
    todo.append(lambda: proc(1))
    proc = getFunc(2)
    todo.append(lambda: proc(1))
    proc = getFunc(3)
    todo.append(lambda: proc(1))

    todo.append(lambda: getFunc(1)(1))
    todo.append(lambda: getFunc(2)(1))
    todo.append(lambda: getFunc(3)(1))

    for task in todo:
        print task()

-----------
the program outputs:
4
4
4
2
3
4
in Python 2.6.
is that excpected?





More information about the Python-list mailing list