我試圖在字典candlebuffer
中保存對象candle
,但它給了我下面的錯誤。我掙扎着;我的語法有什麼不正確?python在字典中的對象
class Observer:
def __init__(self):
self.listeners = []
def attach(self, listener):
if not listener in self.listeners:
self.listeners.append(listener)
def notify(self, event):
for listener in self.listeners:
listener.update(event)
class CandleGenerator(Observer):
def __init__(self,freq):
Observer.__init__(self)
self.freq = freq
self.candle = Candle()
def update(self,tick):
self.candle.addTick(tick,self.freq)
if keyfunc(self.candle.timestamp,self.freq) != self.candle.ref_timestamp:
self.notify(self.candle)
self.candle = Candle()
class CandlePrinter:
def update(self, candle):
print "Bougie>>>>>> freq: %s %s %s %s %s %s %s %s %s " % (candle.freq,candle.last_price,candle.volume, candle.timestamp, candle.ref_timestamp, candle.open_price,candle.high_price,candle.low_price, candle.last_price)
class CandleBuffer:
def __init__(self,candle):
self.candlebuffer={0: candle}
def update(self,candle):
self.candlebuffer[candle.timestamp]= candle
print self.candlebuffer
print('begin')
tickGenerator = TickGenerator()
candleGenerator1 = CandleGenerator(1)
candlePrinter = CandlePrinter()
candleBuffer = CandleBuffer(5)
tickGenerator.attach(candleGenerator1)
candleGenerator1.attach(candlePrinter)
candleGenerator1.attach(candleBuffer)
tickGenerator.generate()
它提供了以下的輸出:
TypeError: __init__() takes exactly 2 arguments (1 given)
您還沒有向我們展示,實際上導致該錯誤的代碼。堆棧跟蹤會告訴您它發生的確切路線。請給*整個*堆棧跟蹤,而不僅僅是錯誤消息,並且包含帶有錯誤的實際行。 – 2013-04-07 13:44:34
如果這是2.x,那麼你的類應該從'object'繼承來支持「new」風格的類。你能展示更多的代碼嗎?即使用該類的代碼。 – 2013-04-07 13:49:20
錯誤來自這個執行線的確如此:candleBuffer = CandleBuffer() – matel 2013-04-07 13:50:07