说,我有一个类: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包含了所有的功能 (我不是一个Python的人这么温柔)
您使用的是什么版本的Python? – detly 2011-03-30 03:57:49
2.6和铁蟒(clr 4.0/dlr) – 2011-03-30 08:35:53