函数对象和闭包
函数对象
# 1. 引用
func = f1
print('f1:', f1)
print('func:', func)
func()
# 2.当作容器类元素
lt = [f1, 1, 2, 3]
print('lt[0]', lt[0])
print('f1', f1)
lt[0]()
# 3. 当作函数参数
def f2(f2_f1):
print('f2_f1',f2_f1)
f2_f1()
f2(f1)
print('f1', f1)
# 4. 当作函数的返回值
def f2(f2_f1):
return f2_f1
闭包函数
闭(封闭/关闭)包(包子/把馅(闭包函数内部的变量)和皮(闭包函数内部的函数)包起来) --》返回闭包函数内部的函数
闭包函数内部的变量+闭包函数内部的函数,这两者包裹在一起
import requests
def f1(url): # f1就叫做闭包函数
def spider():
requests.get(url)
print(url)
return spider # 函数对象
taobao = f1('www.taobao.com')
taobao()