码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-02-08 15:26:31      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:log   elf   exce   调用   val   cat   something   str   ini   

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 装饰器装饰类中的方法(转)

标签:log   elf   exce   调用   val   cat   something   str   ini   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!