内容概要
函数对象
函数对象指的是函数可以被当做 ‘数据’ 来处理,具体可以分为四个方面的使用
函数可以被引用
1 2 3 4 5
| def add(x,y): return x+y func = add func(1,2) >>>3
|
函数可以作为容器类型的元素
1 2 3 4 5
| dic = {'add' : add, 'max' : max} dic >>>{'add': <function add at 0x100661e18>, 'max': <built-in function max>} dic['add'](1,2) >>> 3
|
函数可以作为参数传入另一个函数
1 2 3 4
| def foo(x,y,func): return func(x,y) foo(1,2,add) >>> 3
|
函数的返回值也可以是一个函数
1 2 3 4 5
| def bar(): return add func = bar() func(1,2) >>> 3
|
闭包函数
闭与包
1 2 3 4 5 6 7 8 9 10 11 12 13
|
x = 1 def f1(): def f2(): print(x)
return f2
def f3(): x=3 f2=f1() f2()
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| f3()
也就是说:函数被当做数据处理时,始终以'自带的作用域'为准
若内嵌函数包含对外部函数作用域(而非全局作用域)中变量的引用,那么该'内嵌函数'就是'闭包函数',简称闭包
x=1 def outer(): x=2 def inner(): print(x) return inner
func=outer() func()
|
1 2 3 4 5 6 7
| 可以通过函数的closure属性,查看到闭包函数所包裹的外部变量
func.__closure__ >>> (<cell at 0x10212af78: int object at 0x10028cca0>,) func.__closure__[0].cell_contents >>> 2
|
1 2 3 4 5
| "闭":代表函数是内部的 "包":代表函数外'包裹'着对外层作用域的引用
所以 ---> 无论在哪调用闭包函数,使用的仍然是包裹在其外层的变量
|
闭包的用途
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| 目前为止 我们得到了两种为函数体传值的方式
import requests
def get(url): return requests.get(url).text
def page(url): def get(): return requests.get(url).text return get
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| 提示:requests模块是用来模拟浏览器向网站发送请求并将页面内容下载到本地,需要事先安装:pip3 install requests
对比两种方式:
get('https://www.python.org') get('https://www.python.org') get('https://www.python.org') ……
|
1 2 3 4 5 6 7
|
python=page('https://www.python.org') python() python() python() ……
|
闭包函数的这种特性有时又称为惰性计算。使用将值包给函数的方式,在接下来的装饰器中也将大有用处