我正在爲必須檢查父方法(在裝飾類的父類中同名的方法)編寫方法的裝飾器。從裝飾器訪問擁有裝飾方法的類
例(從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