Python动态添加装饰器到类中通过装饰类的方法
假设我有一个类:
class x:
def first_x_method(self):
print 'doing first_x_method stuff...'
def second_x_method(self):
print 'doing second_x_method stuff...'
这个装饰器
class logger:
@staticmethod
def log(func):
def wrapped(*args, **kwargs):
try:
print "Entering: [%s] with parameters %s" % (func.__name__, args)
try:
return func(*args, **kwargs)
except Exception, e:
print 'Exception in %s : %s' % (func.__name__, e)
finally:
print "Exiting: [%s]" % func.__name__
return wrapped
我将如何编写另一个装饰器 otherdecorator
这样:
@otherdecorator(logger.log)
class x:
def first_x_method(self):
print 'doing x_method stuff...'
def first_x_method(self):
print 'doing x_method stuff...'
相同
class x:
@logger.log
def first_x_method(self):
print 'doing first_x_method stuff...'
@logger.log
def second_x_method(self):
print 'doing second_x_method stuff...'
或实际上替换
@otherdecorator(logger.log)
class x:
为
@otherdecorator
class x:
与otherdecorator 包含所有功能的位置 otherdecorator (我不是蟒蛇人所以要温柔)
say I have a class:
class x:
def first_x_method(self):
print 'doing first_x_method stuff...'
def second_x_method(self):
print 'doing second_x_method stuff...'
and this decorator
class logger:
@staticmethod
def log(func):
def wrapped(*args, **kwargs):
try:
print "Entering: [%s] with parameters %s" % (func.__name__, args)
try:
return func(*args, **kwargs)
except Exception, e:
print 'Exception in %s : %s' % (func.__name__, e)
finally:
print "Exiting: [%s]" % func.__name__
return wrapped
how would I write another decorator otherdecorator
so that:
@otherdecorator(logger.log)
class x:
def first_x_method(self):
print 'doing x_method stuff...'
def first_x_method(self):
print 'doing x_method stuff...'
the same as
class x:
@logger.log
def first_x_method(self):
print 'doing first_x_method stuff...'
@logger.log
def second_x_method(self):
print 'doing second_x_method stuff...'
or in fact replace
@otherdecorator(logger.log)
class x:
with
@otherdecorator
class x:
where otherdecorator contains all the functionality
(I'm not a python person so be gentle)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非有明确的理由使用类作为装饰器,否则我认为使用函数来定义装饰器通常更容易。
这是创建类装饰器
trace
的一种方法,它使用log
装饰器装饰类的所有方法:
Unless there is a definite reason to use a class as a decorator, I think it is usually easier to use functions to define decorators.
Here is one way to create a class decorator
trace
, which decorates all methods of a class with thelog
decorator:yields:
这是作为类实现的
trace
装饰器的一个版本,它允许要求的其他用例:传入函数来装饰被装饰类的所有成员函数。Here's a version of the
trace
decorator implemented as a class which allows for the other use case asked for: passing in the function to decorate all member functions of the decorated class with.