装饰器:设计器(decorator)
给不同的函数(方法用),可以增加不同的公用的功能
@classmethod
@staticmethod
#函数执行后返回了另外一个函数对象
def pri():
return sum #不加括号()就是一个函数对象,加了括号就是调用sum()也就是返回结果
print(type(pri))
#函数的参数可以使用函数对象
def func1(func):
return func([1,2,3]) #func([1,2,3])等同于sum([1,2,3])
#sum->sum([1,2,3])
#pri->pri([1,2,3]) ---->不符合函数定义
print(func1(sum))
print(func1(pri)) # pri为自定义函数,pri里面的sum本身就是自带函数,这种嵌套传参语法不支持
- 函数名-->函数对象
>>> sum à函数对象
<built-in function sum>
- 函数名()-->调用函数
>>> sum([1,2]) à函数调用
3
def add(l):
result = 0
for i in l:
result += i
return result
func1(add)
闭包:
函数对象+函数内部需要使用的一个外部变量=函数
返回的整体
函数调用外部变量的过程:首先从自己内部里面找,然后去外面找
闭包执行示例:
def func():
x = 100
def func1():
print(x)
return func1 #返回的是一个函数对象
a=func() #func()等同于调用func1
print(a) #打印 func1
a() #a()等同于func1(),调用里面的方法
#这个过程等同于 == func()()
输出结果
>>> a=func()
>>> print(a)
<function func.<locals>.func1 at 0x000001A555D680D0>
>>> a()
100
闭包解析过程:
func()() #示例
>>> func()()
100
def func():
x = 100
def func1():
print(x)
return func1() #这里返回的是func1()调用了,并非func1对象
print(func()())
输出结果
D:\>py b.py
100
Traceback (most recent call last):
File "b.py", line 7, in <module>
print(func()())
TypeError: 'NoneType' object is not callable
过程解析:
>>> def func():
... x = 100
... def func1():
... print(x)
... return func1
... #函数名—》返回了函数对象 return fuc1
#函数名(参数或无参数)--》调用函数
return func1()-->打印100,返回None#这里返回的是func1()调用了,并非func1对象
#函数对象:函数所在的内存地址
>>> func()
100
>>> a = func()
100
>>> type(a) # 因为func1里面没有返回值return,所以此时的a就是None
<class 'NoneType'>
>>> a()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
闭包部分:一个函数加上函数执行所需的一个外部变量
x = 100
def func1():
print(x)
#以上是闭包部分
查看闭包变量
def func():
x = 100
def func1():
print(x)
return func1
#闭包部分:--》赋值各了a,
也就是x = 100和func1的内存地址赋值给了a
a = func()
print(a.__closure__)
输出结果:
D:\>py b.py
(<cell at 0x000001830F79A6D8: int object at 0x00007FFC1E4DE080>,)
func() à func函数调用后,返回func1 + x
a = func() à a = func1,把func1 + x 赋值给a
a() ---》调用func1()-->打印了100,返回了None
面试闭包:
闭包:函数加上函数所需要的变量作为一个整体返回(打包返回)
函数+需要的外部变量,一起返回了
装饰器:给函数加一些通用的功能。
计算一下不同函数的执行时间。
import time
#增加计时功能
def a():
time1 = time.time()
i = 1
while i <1000000:
i+=1
time2 = time.time()
print("time elapsed:%s"%(time1 - time2))
def b():
time1 = time.time()
i = 1
while i < 9999999:
i+=1
time2 = time.time()
return print("time elapsed:%s"%(time1 - time2))
#通过函数来解决:
def timer(func):
time1 = time.time()
func() #做了一次函数调用
time2 = time.time()
print("time elapsed:%s"%(time2-time1))
def a():
i = 1
while i <1000000:
i+=1
def b():
i = 1
while i < 9999999:
i+=1
timer(a)
timer(b)
-------------------------------
import time
def timer(func):
time1 = time.time()
func() #做了一次函数调用
time2 = time.time()
print("time elapsed:%s"%(time2-time1))
@timer
def a():
i = 1
while i <1000000:
i+=1
@timer
def b():
i = 1
while i < 9999999:
i+=1
a()
import time
def timer(func): #调用里面的func()
def func1():
time1 = time.time()
func() #做了一次函数调用
time2 = time.time()
print("time elapsed:%s"%(time2-time1))
return func1
#闭包:func+func1
@timer
def a():
i = 1
while i <1000000:
i+=1
"""
@timer
def b():
i = 1
while i < 9999999:
i+=1
"""
a #a等价与调用timer(a)
a() #a()等价与调用timer(a)()
"""
a() 整个过程就是:timer(func)函数参数传入--->a()函数,传入到func1中的func(),
实现了一次a()的函数调用,
"""