為什么這樣就錯了?
def count():
? ? fs = []
? ? for i in range(1, 4):
? ? ? ? def f(j):
? ? ? ? ? ? def g(j):
? ? ? ? ? ? ? ? return j*j
? ? ? ? ? ? return g
? ? ? ? r = f(i)
? ? ? ? fs.append(r)
? ? return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
報錯信息:Traceback (most recent call last):
? File "index.py", line 21, in?
? ? print f1(), f2(), f3()
TypeError: () takes exactly 1 argument (0 given)
2019-04-20
你的g(j)中要求有一個參數,但是你接受f1=g1再調用f1時沒有傳一個參數,所以報錯。你這里應該把g(j)中的參數去掉。
2019-04-29
def count():
??? fs = []
??? for i in range(1, 4):
??????? def f(x):
?????? ??? ?return x*x
??????? fs.append(f)
??? return fs
f1, f2, f3 = count()
print f1(1), f2(2), f3(3)