前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 装饰器使用总结

python 装饰器使用总结

作者头像
授客
发布2019-10-29 11:51:31
3110
发布2019-10-29 11:51:31
举报
文章被收录于专栏:授客的专栏

测试环境

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-10-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 例1:一个简单的例子
  • 例2:装饰带参数函数
  • 例3:函数被多给装饰器方法装饰
  • 例4:在类中使用装饰器
  • 例5:装饰器方法也可以是类函数
  • 需要注意的点
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档