【发布时间】:2018-11-14 16:26:54
【问题描述】:
我正用头撞桌子,试图让你明白我做错了什么。
class Endpoint:
PortID = 0
MAC = ''
IP = ''
FQDN = ''
__init__()...
class Interface:
Index = 0
Endpoints = []
__init__()
...
我的主要部分:
for i in interfaces: #list of Interface objects
for e in endpoints: #list of Endpoint objects
is (some condidions):
i.Endpoints.append(e)
i.print() # prints all endpoints connected to the interface
然后在输出中我有这样的东西:
ge0/0:
11:11:11:11:11:11
ge0/1:
11:11:11:11:11:11
22:22:22:22:22:22
ge0/2
11:11:11:11:11:11
22:22:22:22:22:22
33:33:33:33:33:33
我打算得到的是:
ge0/0:
11:11:11:11:11:11
ge0/1:
22:22:22:22:22:22
ge0/2
33:33:33:33:33:33
你明白了。这就像“i”变量不是在每次循环迭代中创建和销毁,而是只是更新。换句话说,它不是接口对象的表示,而是某种缓冲区commot 到整个循环。 我究竟做错了什么?我怎样才能达到预期的目标?
提前感谢您的帮助。
【问题讨论】:
标签: python list loops variables scope