【发布时间】:2011-07-25 19:04:45
【问题描述】:
说我有课:
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?
-
2.6 和 Iron Python (clr 4.0/dlr)