首页 > 编程语言 > 详细

Python 装饰器装饰类中的方法(转)

时间:2018-02-08 15:09:40      阅读:219      评论:0      收藏:0      [点我收藏+]

def catch_exception(origin_func):
def wrapper(self, *args, **kwargs):
try:
u = origin_func(self, *args, **kwargs)
return u
except Exception:
self.revive() #不用顾虑,直接调用原来的类的方法
return ‘an Exception raised.‘
return wrapper
class Test(object):
def init(self):
pass
def revive(self):
print(‘revive from exception.‘)
# do something to restore
@catch_exception
def read_value(self):
print(‘here I will do something.‘)
# do something.

原文地址:https://kingname.info/2017/04/17/decorate-for-method/

Python 装饰器装饰类中的方法(转)

原文:https://www.cnblogs.com/qdyxmas/p/8431353.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!