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

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/

posted @ 2018-02-08 14:21  ~草帽小子~  阅读(135)  评论(0编辑  收藏  举报