我正在为必须检查父方法(在装饰类的父类中同名的方法)编写方法的装饰器。从装饰器访问拥有装饰方法的类
例(从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
所以我在寻找的代码代替#我在这里要输入...
感谢。
我没有直接使用你的代码,因为我的问题比我告诉你的更具体。但是,在遍历* dct *时使用* name *有点奇怪,因为* name *通常用于* type *构造。谢谢,我认为无论如何接受你的答案都可以,因为它对我有很大的帮助。 – Gra 2009-04-16 13:08:27