【发布时间】:2020-09-01 12:59:56
【问题描述】:
我在将变量 self.data_counter 存储在另一个变量(例如 prev_counter_data)中时遇到了问题。
我根本无法存储当前值 - 我分配给 self.data_counter 的变量总是以某种方式具有完全相同的值。
class SimpleSwitch(app_manager.RyuApp):
def __init__(self, *args, **kwargs):
super(SimpleSwitch, self).__init__(*args, **kwargs)
self.data_counter = {}
self.monitor_thread = hub.spawn(self._monitor)
def _monitor(self):
timer = 0
prev_data_counter = self.data_counter
while True:
hub.sleep(5)
timer = timer + 5
# assign current values to self.data_counter in func()
func()
# compare current values to previous values
if (timer%60) == 0:
prev_data_counter = self.data_counter
知道我的想法的缺陷在哪里吗?为什么prev_data_counter 总是等于self.data_counter?
字典self.data_counter包含诸如[(id, port):traffic]之类的信息。我可以通过self.data_counter[id][port] = traffic 赋值。
【问题讨论】:
-
self.data_counter在 while 循环中不断变化。prev_data_counter应每 60 秒更新一次,以匹配self.data_counter的值。
标签: python variables updates store self