'''闭包就是内层函数使用了外层函数中的变量。'''
def outer():
a = 10
def inner():
print(a) # 这里就是闭包
return inner # 闭包通常都是返回内层函数
a = outer()
a() # 10
'''闭包的作用:
1、不许外面改变这个变量。
2、让这个变量常驻于内存。
'''
普通函数实例:
def func(a):
print(a)
print(dir(func))
'''打印结果:
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__',
'__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__',
'__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']'''
print('__closure__' in dir(func)) # True
print(func.__closure__) # None
闭包函数实例:
def wrapper(a):
b = 20
def inner():
print(b)
return inner
print(wrapper.__closure__) # None
f = wrapper(10)
print(f.__closure__) # (<cell at 0x0000000005E28768: int object at 0x000000001D737040>,)
print(f.__closure__[0].cell_contents) # 20
for cell in f.__closure__:
print(cell.cell_contents) # 20
装饰器的作用:在不修改原来函数程序的情况下,给函数增加功能。开放封闭原则,对功能开放,对程序封闭。
'''装饰器的固定格式'''
def wrapper(func):
def inner(*args, **kwargs):
print("执行前")
ret = func(*args, **kwargs)
print("执行后")
return ret
return inner
@wrapper #func = wrapper(func) ==> 目标函数 = 装饰器函数(目标函数)
def func():
print("func is running...")
func()
执行结果:
执行前
func is running...
执行后
'''带参数的装饰器'''
def wrapper(flag):
def outer(func):
def inner(*args, **kwargs):
if flag:
print("在之前.")
ret = func(*args, **kwargs)
print("在之后")
return ret
else:
return func(*args, **kwargs)
return inner
return outer
@wrapper(1) # 先执行wrapper(1),返回outer,@outer就是语法糖 ==》wrapper(1) ==>outer
def func():
print("func is running...")
@wrapper(0) #先执行wrapper(0),返回outer,@outer就是语法糖
def func2():
print("func2 is running...")
func()
func2()
执行结果:
在之前.
func is running...
在之后
func2 is running..
'''写日志'''
def record_log(file):
def log(fn):
def inner(*args, **kwargs):
ret = fn(*args, **kwargs)
with open(file, mode="a", encoding="utf-8") as f: #记录日志
f.write("调用函数: %s" % fn.__name__)
return ret
return inner
return log
@record_log('func1.txt')
def func1(): # 日志放在func1.txt
print("我是func1")
@record_log('func2.txt')
def func2(): # 日志放在func2.txt
print("我是func2")
func1()
func2()
执行结果:
我是func1
我是func2
'''同一个函数被多个装饰器修饰'''
def wrapper1(func):
def inner(*args, **kwargs):
print("执行wrapper1前")
ret = func(*args, **kwargs)
print("执行wrapper1后")
return ret
return inner
def wrapper2(func):
def inner(*args, **kwargs):
print("执行wrapper2前")
ret = func(*args, **kwargs)
print("执行wrapper2后")
return ret
return inner
def wrapper3(func):
def inner(*args, **kwargs):
print("执行wrapper3前")
ret = func(*args, **kwargs)
print("执行wrapper3后")
return ret
return inner
@wrapper1 #3.装饰wrapper2后的结果
@wrapper2 #2.装饰wrapper3后的结果
@wrapper3 #1.装饰func函数
def func():
print("func is running...")
func()
执行结果:
执行wrapper1前
执行wrapper2前
执行wrapper3前
func is running...
执行wrapper3后
执行wrapper2后
执行wrapper1后