3

为什么方法更新中没有定义变量freq?我在 init 上调用方法蜡烛,这个方法包含频率?

class candle:
    def __init__(self):
        self.freq = None
        self.open_price = None
        self.high_price = None
        self.low_price = None
        self.last_price = None
        self.volume = 0

    def addTick(self,tick):
        if self.open_price is None:
            self.open_price = tick.price
        if self.high_price is None or tick.price >self.high_price:
            self.high_price = tick.price
        if self.low_price is None or tick.price <self.low_price:
            self.low_price = tick.price   
        self.last_price = tick.price
        self.volume = self.volume +1


    def update(self,tick):
        self.candle.addTick(tick)
        if keyfunc(current_time,freq) != reference_timestamp[freq]:
            self.notify(self.candle)
            self.candle = candle()
4

2 回答 2

2

Python中没有隐self式,访问成员变量时必须显式。

if keyfunc(current_time,freq) != reference_timestamp[freq]:

应该

if keyfunc(current_time, self.freq) != reference_timestamp[self.freq]:

不确定在哪里reference_timestamp定义,假设它是全局的。

于 2013-04-06T12:58:12.783 回答
2

您正在尝试访问局部变量(未在该范围内定义)而不是类实例属性。尝试使用 self.freq 而不是 freq。

于 2013-04-06T12:59:01.127 回答