def calc_prod(lst): def lazy_prod(): def f(x, y): return x * y return reduce(f, lst, 1) return lazy_prod f = calc_prod([1, 2, 3, 4]) print (f()) Traceback (most recent call last): File "<ipython-input-28-5166a7a3086
def calc_prod(lst):
? ? def lazy_prod():
? ? ? ? def f(x, y):
? ? ? ? ? ? return x * y
? ? ? ? return reduce(f, lst, 1)
? ? return lazy_prod
f = calc_prod([1, 2, 3, 4])
print (f())
Traceback (most recent call last):
? File "<ipython-input-28-5166a7a30861>", line 9, in <module>
? ? print (f())
? File "<ipython-input-28-5166a7a30861>", line 5, in lazy_prod
? ? return reduce(f, lst, 1)
NameError: name 'reduce' is not defined
2018-08-23
你用的是python2的語(yǔ)法,但是你的環(huán)境是python3的環(huán)境。reduce函數(shù)在python3的內(nèi)建函數(shù)移除了,放入了functools模塊??梢栽谇懊嫣砑舆@樣一行代碼: