Python装饰器,大致可分为:无参装饰器、带参装饰器。接下来我们一探究竟
"""
装饰器的具体使用
"""
print("# -------------------- 多个装饰器一起使用 -------------------- #")
# 加粗
def make_bold(fn):
def wrapped():
return "<b>" + fn() + "</b>"
return wrapped
# 斜体
def make_italic(fn):
def wrapped():
return "<i>" + fn() + "</i>"
return wrapped
@make_bold
def test1():
return "hello world-1"
@make_italic
def test2():
return "hello world-2"
# 两个装饰器一起使用
@make_bold
@make_italic
def test3():
return "hello world-3"
print(test1())
print(test2())
print(test3())运行结果:
# -------------------- 多个装饰器一起使用 -------------------- #
<b>hello world-1</b>
<i>hello world-2</i>
<b><i>hello world-3</i></b>可以发现装饰 test3 的结果是先变斜体,然后在加粗。首先程序是从上到下执行的,当遇到 @make_bold 时它会把下面的函数引用传递给 make_bold 函数,但下面的又是一个装饰器 @make_italic ,这个装饰器一样会把下面的函数 test3 传递给 make_italic 函数,先将make_italic 函数的返回值 wrapped 赋值给 test3,然后这个新的 test3 函数再传递给 make_bold。因此是先变斜体,然后再加粗。
可能理解起来会很绕,看看下面这段程序结果你就更明白了,最好是亲自去尝试一下,加强理解。
"""
装饰器的具体使用
"""
print("# -------------------- 多个装饰器一起使用 -------------------- #")
# 加粗
def make_bold(fn):
print("make_bold called")
print(fn.__name__)
def wrapped():
return "<b>" + fn() + "</b>"
return wrapped
# 斜体
def make_italic(fn):
print("make_italic called")
print(fn.__name__)
def wrapped():
return "<i>" + fn() + "</i>"
return wrapped
# 两个装饰器一起使用
@make_bold
@make_italic
def test3():
return "hello world-3"
print(test3())运行结果:
# -------------------- 多个装饰器一起使用 -------------------- #
make_italic called
function name: test3
make_bold called
function name: wrapped
<b><i>hello world-3</i></b>先传递 test3 函数,经过 @make_italic 装饰器后 test3 = wrapped,在传递 test3 函数,而此时test3 其实就是 wrapped。因此第二次 fn.__name__,打印是 wrapped。
print("# -------------------- 无参数装饰器 -------------------- #")
from time import ctime, sleep
# 打印当前运行时间
def cur_time(func):
def wrapped_func():
print("%s called at %s" % (func.__name__, ctime()))
func()
return wrapped_func
@cur_time
def foo():
print("I am foo")
foo()
sleep(2)
foo()上面代码理解装饰器执行行为可理解成
# foo先作为参数赋值给func后,foo接收指向timefun返回的wrapped_func
foo = timefun(foo)
# 调用foo(),即等价调用wrapped_func()
foo()
# 内部函数wrapped_func被引用,所以外部函数的func变量(自由变量)并没有释放
# func里保存的是原foo函数对象运行结果:
# -------------------- 无参数装饰器 -------------------- #
foo called at Thu Apr 15 20:40:35 2021
I am foo
foo called at Thu Apr 15 20:40:37 2021
I am fooprint("# -------------------- 被装饰的函数有参数 -------------------- #")
from time import ctime, sleep
def cur_time(func):
# 注意在这里接受函数参数
def wrapped_func(a, b):
print("%s called at %s" % (func.__name__, ctime()))
print(a, b)
func(a, b)
return wrapped_func
@cur_time
def foo(a, b):
print(a + b)
foo(3, 5)
sleep(2)
foo(2, 4)运行结果:
# -------------------- 被装饰的函数有参数 -------------------- #
foo called at Thu Apr 15 20:47:18 2021
3 5
8
foo called at Thu Apr 15 20:47:20 2021
2 4
6print("# -------------------- 被装饰的函数有不定长参数 -------------------- #")
from time import ctime, sleep
def cur_time(func):
# 这里用*arg **kwargs来接受不定长参数
def wrapped_func(*args, **kwargs):
print("%s called at %s" % (func.__name__, ctime()))
print("args:", args)
print("kwargs:", kwargs)
func(*args, **kwargs)
return wrapped_func
@cur_time
def foo(a, b, c):
print(a + b + c)
foo(1, 3, 5)
sleep(2)
foo(2, 4, c=6)运行结果:
# -------------------- 被装饰的函数有不定长参数 -------------------- #
foo called at Thu Apr 15 21:10:02 2021
args: (1, 3, 5)
kwargs: {}
9
foo called at Thu Apr 15 21:10:05 2021
args: (2, 4)
kwargs: {'c': 6}
12以 foo(2, 4, c=6) 举例,2, 4 会以元组的形式传递给 args,c=6 这种键值对的则是以字典的形式传递给 kwargs ,需注意的是在函数参数定义的时候需在参数名前带上 *。参数名称可以自定义但 * 不能少,否则就不是不定参数函数。例如 *args 可以换成 *params ,*kwargs 可以换成 *kws。但我们最好不要改动。
print("# -------------------- 装饰器的return -------------------- #")
from time import ctime, sleep
def cur_time(func):
def wrapped_func():
print("%s called at %s" % (func.__name__, ctime()))
func()
return wrapped_func
@cur_time
def foo():
print("I am foo")
@cur_time
def get_info():
return '----hei hei---'
foo()
sleep(2)
foo()
print(get_info())执行结果:
# -------------------- 装饰器的return -------------------- #
foo called at Thu Apr 15 21:27:52 2021
I am foo
foo called at Thu Apr 15 21:27:54 2021
I am foo
get_info called at Thu Apr 15 21:27:54 2021
None如果修改装饰器为如下
def cur_time(func):
def wrapped_func():
print("%s called at %s" % (func.__name__, ctime()))
ret = func() # 先接收函数的返回值
return ret # 然后返回出去
return wrapped_func则运行结果:
# -------------------- 装饰器的return -------------------- #
foo called at Thu Apr 15 21:32:43 2021
I am foo
foo called at Thu Apr 15 21:32:45 2021
I am foo
get_info called at Thu Apr 15 21:32:45 2021
----hei hei---说明:
一般情况下为了让装饰器更通用,可以有
return
print("# -------------------- 装饰器带参数 -------------------- #")
from time import ctime, sleep
def time_arg(pre="hello"):
def cur_time(func):
def wrapped_func():
print("%s called at %s %s" % (func.__name__, ctime(), pre))
return func()
return wrapped_func
return cur_time
@time_arg("hui")
def foo():
print("I am foo")
@time_arg("python")
def goo():
print("I am goo")
foo()
sleep(2)
foo()
goo()
sleep(2)
goo()装饰过程如下
1. 先调用 time_arg("hui")
2. 将步骤1得到的返回值,即cur_time返回, 然后装饰器@time_arg('hui')就变成了@cur_time
3. 将 foo 传递给 cur_time
3. 将 cur_time(foo)的结果返回,即wrapped_func
4. 让foo = wrapped_fun,即foo现在指向wrapped_func可以理解为
foo() == timef_arg("hui")(foo)()
goo() == timef_arg("python")(goo)()记住
xxx()即xxx调用,而@decorate则执行decorate函数,只不过会把@下面被装饰的函数当做参数传递给decorate函数执行。
装饰器函数其实是这样一个接口约束,它必须接受一个 callable 对象作为参数,然后返回一个 callable 对象。在Python中一般 callable 对象都是函数,但也有例外。只要某个对象重写了 __call__() 方法,那么这个对象就是 callable 的。
callable 就是可被调用执行的对象class Test():
def __call__(self):
print('call me!')
t = Test()
t() # 输出 call me类装饰器demo
class Test(object):
def __init__(self, func):
print("---初始化---")
print("func name is %s" % func.__name__)
self.__func = func
def __call__(self):
print("---装饰器中的功能---")
self.__func()
@Test
def test():
print("----test---")
test()说明:
1. 当用Test来装作装饰器对test函数进行装饰的时候,首先会创建Test的实例对象
并且会把test这个函数名当做参数传递到__init__方法中
即在__init__方法中的属性__func指向了test指向的函数
2. test指向了用Test创建出来的实例对象
3. 当在使用test()进行调用时,就相当于让这个对象(),因此会调用这个对象的__call__方法
4. 为了能够在__call__方法中调用原来test指向的函数体,
所以在__init__方法中就需要一个实例属性来保存这个函数体的引用
所以才有了self.__func = func这句代码,从而在调用__call__方法中能够调用到test之前的函数体运行结果如下:
---初始化---
func name is test
---装饰器中的功能---
----test---源代码已上传到 Gitee PythonKnowledge: Python知识宝库,欢迎大家来访。
✍ 码字不易,还望各位大侠多多支持❤️。
新建文件夹X
大自然用数百亿年创造出我们现实世界,而程序员用几百年创造出一个完全不同的虚拟世界。我们用键盘敲出一砖一瓦,用大脑构建一切。人们把1000视为权威,我们反其道行之,捍卫1024的地位。我们不是键盘侠,我们只是平凡世界中不凡的缔造者 。