gpt4 book ai didi

python undefined variable

转载 作者:太空宇宙 更新时间:2023-11-03 15:20:29 26 4
gpt4 key购买 nike

为什么方法更新中没有定义变量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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com