首页 > 如何在for循环中根据循环变量创建新函数

如何在for循环中根据循环变量创建新函数

首先谢谢关注。倒腾了一个下午,最后确定了问题在于for循环中创建的函数中变量的值会被覆盖。如下列代码所示:

    funcList = []
    i_List = []
    for i in range(0, 3):
        i_List.append(i)
        def printI():
            print(i_List[i], end = ',')
        funcList.append(printI)
    
    print(funcList[0]() )
    print(funcList[1]() )
    print(funcList[2]() )
    print(i_List)

程序运行结果如下:

我的问题是:

谢谢!


为什么会有个None

因为print(funcList[0]())语句中, funcList[0]()的返回值是None:

def printI():
    # 这里打印了i_List[i], 加一个括号, 但是不换行
    print(i_List[i], end = ',')
    # 所有不显示返回的返回值都是None
    # 外层再有一个print()的话,就会打印`None`
如何在for循环中利用循环变量批量创建函数

这是个陷阱,情况跟Javascript类似。printI这个闭包捕获了i这个变量(捕获的是变量本身而不是变量的值), 但是i变量是变化的, 所以最后的结果都是i在迭代后的最后一个值。
因此,有几个办法可以绕过(不限于以下几种):

1.

...
    def printI(x):
        def _printI():
            print(i_List[x], end = ',')
        return _printI
    funcList.append(printI(i))
...

2.

from functools import partial
...
    def printI(x):
        print(i_List[x], end = ',')
    funcList.append(partial(printI, i))
...

3.

...
    def printI(i=i):
        print(i_List[i], end = ',')
    funcList.append(printI)
...
【热门文章】
【热门文章】