python未定义的变量

时间:2013-04-06 12:51:34

标签: python debugging

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

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()

2 个答案:

答案 0 :(得分:2)

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

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

应该是

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

不确定定义reference_timestamp的位置,假设它是全局的。

答案 1 :(得分:2)

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