Generating functions inside loop with lambda expression in python

前端 未结 6 622
眼角桃花
眼角桃花 2020-11-30 08:37

If I make two lists of functions:

def makeFun(i):
    return lambda: i

a = [makeFun(i) for i in range(10)]
b = [lambda: i for i in range(10)]
6条回答
  •  悲哀的现实
    2020-11-30 09:14

    As others have stated, scoping is the problem. Note that you can solve this by adding an extra argument to the lambda expression and assigning it a default value:

    >> def makeFun(i): return lambda: i
    ... 
    >>> a = [makeFun(i) for i in range(10)]
    >>> b = [lambda: i for i in range(10)]
    >>> c = [lambda i=i: i for i in range(10)]  # <-- Observe the use of i=i
    >>> a[2](), b[2](), c[2]()
    (2, 9, 2)
    

    The result is that i is now explicitly placed in a scope confined to the lambda expression.

提交回复
热议问题