python undefined variable

标签 python debugging

为什么方法更新中没有定义变量freq? 我在 init 上调用 candle 方法,这个方法包含 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()

最佳答案

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

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

应该是

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

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

关于python undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15851185/

相关文章:

python - unicode 到 python 对象转换

visual-studio - 如何在 Windows CE 设备上调试正在运行的 .NET 程序

Android NDK 找到动态链接 : unable to debug library

python - Python 中的并行程序不产生输出

python - 如何在python中用字符串轴而不是整数绘制混淆矩阵

javascript - Node.js 远程调试不起作用

node.js - 无法使用 Node 检查器 : websocket_closed and Assertion failed: Unknown experiment canvasInspection 调试 Nodejs

c++ - 如何让 Visual Studio 调试器在没有焦点的情况下中断

Python线程中断终端

python - 根据另一个数据框中的列填充数据框中的空值