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.
Python 装饰器装饰类中的方法(转)
Posted qdyxmas
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 装饰器装饰类中的方法(转)相关的知识,希望对你有一定的参考价值。
以上是关于Python 装饰器装饰类中的方法(转)的主要内容,如果未能解决你的问题,请参考以下文章