1. 程式人生 > >python 高階函數:返回函數

python 高階函數:返回函數

color 封存 ima 分享 技術 png urn alt pro

不太理解,先做個記錄

返回函數:函數沒有立刻執行,而是將變量全部封存起來


#!/usr/bin/python

def count():
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
fs.append(f(i))
return fs

f1, f2, f3 = count();
print f1(), f2(), f3()

技術分享圖片

print 'count():', type(count())

print 'f1:', type(f1)
print 'f1():', type(f1())

技術分享圖片


python 高階函數:返回函數