如何强迫Python做正确的事?
这是一种方法:
fun_list = []for i in range(5): def fun(e, _ndx=i): return e[_ndx] fun_list.append(fun)mylist = range(10)print([f(mylist) for f in fun_list])
之所以有效,
_ndx是因为执行
deffor语句时会评估并保存for的默认值
fun。(在python中, 执行
def语句。) __

如何强迫Python做正确的事?
这是一种方法:
fun_list = []for i in range(5): def fun(e, _ndx=i): return e[_ndx] fun_list.append(fun)mylist = range(10)print([f(mylist) for f in fun_list])
之所以有效,
_ndx是因为执行
deffor语句时会评估并保存for的默认值
fun。(在python中, 执行
def语句。) __