【发布时间】:2009-04-15 20:23:35
【问题描述】:
我正在为必须检查父方法(我正在装饰的类的父类中的同名方法)的方法编写装饰器。
示例(来自PEP 318的第四个示例):
def returns(rtype):
def check_returns(f):
def new_f(*args, **kwds):
result = f(*args, **kwds)
assert isinstance(result, rtype), \
"return value %r does not match %s" % (result,rtype)
return result
new_f.func_name = f.func_name
# here I want to reach the class owning the decorated method f,
# it should give me the class A
return new_f
return check_returns
class A(object):
@returns(int)
def compute(self, value):
return value * 3
所以我正在寻找代码来代替 # here I want...
谢谢。
【问题讨论】: