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

Posted qdyxmas

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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/



















以上是关于Python 装饰器装饰类中的方法(转)的主要内容,如果未能解决你的问题,请参考以下文章

python中装饰器装饰类中的方法

类中的Python装饰器

python 装饰器调用其他类中的方法

python装饰器之property

python 类中的某个函数作为装饰器

作为基类一部分的 Python 装饰器不能用于装饰继承类中的成员函数