装饰器本身是一个函数,使用“@”与被装饰函数连接;程序运行装饰器函数时,会先对装饰器函数从上到下解释并存到内存
#!/usr/bin/env python
# coding=utf-8
def outer(fun):
def wrapper(args0):
print('Check')
# 使用result变量接收原函数的返回值,并返回
result = fun(args0)
print('哈哈哒')
return result
return wrapper
"""
@outer = outer(func1)
:表示把被装饰函数当成参数传给装饰器outer,再解释wrapper()函数并放入内存;
解释完wrapper函数后将wrapper函数返回给outer函数
"""
@outer
def func1(args1):
print('func1', args1)
return 'return'
@outer
def func2(args2):
print('func2', args2)
func1('矮油喂')
func2('哈哈哈')