python 装饰器使用总结
by:授客 QQ:1033553122
测试环境
win10
python 3.5
例1:一个简单的例子
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
def wrapper_method1(func):# func用于接收被装饰的函数地址
def wrapper():
print("执行wrapper_method1")
func()#调用被装饰的函数
return wrapper#返回方法地址,供执行被装饰函数前调用
@wrapper_method1#等同于wrapper_method1(myfunction)
def myfuntion():
print("执行myfunction")
myfuntion()
运行结果:
执行wrapper_method1
执行myfunction
例2:装饰带参数函数
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
def wrapper_method1(func):
def wrapper(name, age):#这里的参数列表和myfuntion参数列表保持一致
print("执行wrapper_method1 name:%s age:%s" % (name, age))
func(name, age)#记得给要调用的函数传递参数
return wrapper
@wrapper_method1
def myfuntion(name, age):
print("执行myfunction name:%s age:%s" % (name, age))
myfuntion('shouke', 'unknow')
运行结果:
执行wrapper_method1 name:shouke age:unknow
执行myfunction name:shouke age:unknow
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
def wrapper_method1(func):
def wrapper(*args, **kwargs):
print("执行wrapper_method1 args:", args)
func(*args, **kwargs)
return wrapper
@wrapper_method1
def myfuntion(*args,**kwargs):
print("执行myfunction args:", args)
myfuntion('shouke', 'unknow')
运行结果:
执行wrapper_method1 args: ('shouke', 'unknow')
执行myfunction args: ('shouke', 'unknow')
例3:函数被多给装饰器方法装饰
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
def wrapper_method1(func):
def wrapper(*args, **kwargs):
print("执行wrapper_method1")
func(*args, **kwargs)
return wrapper
def wrapper_method2(func):
def wrapper():
print("执行wrapper_method2")
func()
return wrapper
@wrapper_method1
@wrapper_method2
def myfuntion():
print("执行myfunction")
myfuntion()
运行结果:
执行wrapper_method1
执行wrapper_method2
执行myfunction
说明:装饰器方法执行顺序为从远到近,从上到下。
例4:在类中使用装饰器
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
def wrapper_method1(func):
def wrapper(*args, **kwargs):
print("执行wrapper_method1")
func(*args, **kwargs)
return wrapper
class MyClass:
def __init__(self):
pass
@staticmethod
@wrapper_method1
def myfuntion():
print("执行myfunction")
MyClass.myfuntion()
运行结果:
执行wrapper_method1
执行myfunction
例5:装饰器方法也可以是类函数
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
class MyClass2:
@staticmethod
def wrapper_method1(func):
def wrapper(*args, **kwargs):
print("执行wrapper_method1")
func(*args, **kwargs)
return wrapper
class MyClass:
def __init__(self):
pass
@staticmethod
@MyClass2.wrapper_method1
def myfuntion():
print("执行myfunction")
MyClass.myfuntion()
运行结果:
执行wrapper_method1
执行myfunction
需要注意的点
1、 即便被装饰函数拥有默认值也要显示传递参数,否则报错,如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
def wrapper_method1(func):
def wrapper(name, age):
print("执行wrapper_method1 name:%s age:%s" % (name, age))
func(name, age)
return wrapper
@wrapper_method1
def myfuntion(name='shouke', age='unknow'):
print("执行myfunction name:%s age:%s" % (name, age))
myfuntion()
运行结果:
TypeError: wrapper() missing 2 required positional arguments: 'name' and 'age'
2、 如果被装饰函数为类的静态函数时,@staticmethod必须位于最上方,否则报错,如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'shouke'
def wrapper_method1(func):
def wrapper(*args, **kwargs):
print("执行wrapper_method1")
func(*args, **kwargs)
return wrapper
class MyClass:
def __init__(self):
pass
@wrapper_method1
@staticmethod
def myfuntion():
print("执行myfunction")
MyClass.myfuntion()
运行结果:
Traceback (most recent call last):
执行wrapper_method1
File "E:/PrivateReops/CassTestManage/TMP/backend/mytest.py", line 34, in <module>
MyClass.myfuntion()
File "E:/PrivateReops/CassTestManage/TMP/backend/mytest.py", line 9, in wrapper
func(*args, **kwargs)
TypeError: 'staticmethod' object is not callable