【发布时间】:2015-11-17 14:06:57
【问题描述】:
我正在尝试将数据追加到列表中,但在我追加新值之前,列表似乎已重置。以下是相关代码:
这是主模块:
def update():
global url
data.add_price(df.pulldata(url))
这是数据模块:
historical_price = []
def add_price(price):
historical_price.append(price)
print(historical_price)
我猜测问题在于我将 history_price 定义为顶部为空的事实。然而,这让我感到困惑,因为整个脚本从未运行过。我该如何解决这个问题?提示表示赞赏。
【问题讨论】:
-
add_price和historical_price是一个类的一部分吗?它位于哪里?如果是这种情况,您可能只想使用self.historical_price将historical_price与实例相关联。不确定这是否是您的问题所在... -
请澄清您所说的重置是什么意思?你的意思是它被清空了?如果 data 是一个类实例, add_price 函数应该没有
self属性吗?在我看来historical_price应该是self.historical_price = []并且应该相应地修改函数。 -
请显示更多您的代码,现在类定义甚至不存在。具体来说,这段代码是怎么调用的?我们无法仅凭此重现错误。
-
你怎么知道它被重置了?
-
对不起,我显然是指模块而不是类。我总是让他们感到困惑。 @彼得伍德