幫助一個人。似乎無法得到裝飾工作與繼承。將其分解成我的臨時工作區中最簡單的示例。仍似乎無法得到它的工作。Python裝飾器和繼承
class bar(object):
def __init__(self):
self.val = 4
def setVal(self,x):
self.val = x
def decor(self, func):
def increment(self, x):
return func(self, x) + self.val
return increment
class foo(bar):
def __init__(self):
bar.__init__(self)
@decor
def add(self, x):
return x
糟糕,名稱「裝飾」未定義。
好的,@bar.decor
怎麼樣? TypeError:必須用bar實例作爲第一個參數調用unbound方法「decor」(取而代之的是函數實例)
好的,@self.decor
怎麼樣?名稱「自我」未定義。
好的,@foo.decor
怎麼樣?!名稱「foo」未定義。
AaaaAAaAaaaarrrrgggg ...我做錯了什麼?
在你的榜樣,你可以有'返回X + self.val'在'foo'的add'的'定義。你能不能在你的實際代碼中做到這一點? – 2010-06-08 20:52:16
這是一個精煉的示例代碼,突出顯示了我正面臨的問題。如果代碼很簡單,那麼是的。但是,事實並非如此。 – wheaties 2010-06-08 21:04:10