我正在尝试打印连接到其他节点的节点列表,以便我可以查看它们以调试一些围绕图形的 python 代码。我这样做是为了练习,所以我自己设置节点/边/组。
我尝试使用 for every type 循环打印出与 myNode 类内部的函数的连接。但是如果我指定数组的索引并对其调用相同的方法,我会得到不同的输出。
def print_connections(self):
for node in self.connections:
print(node.name, end = " ")
...
for node in nodes:
node.print_connections()
print(" ") # so I can have a newline in between groups
print(nodes[1].print_connections())
for/each 具有良好的输出,或者看起来不错:
2 5 3 0
这与索引打印的行不同:
2 5 3 0 None
这是 python 中的预期行为吗?我该如何解决这个问题?
your print_connections(self)
不返回任何值,因此返回None
,而你想要做的是print(nodes[1].print_connections())
是打印返回值nodes[1].print_connections()
,这将是None
,
所以你应该做的是,
for node in nodes:
node.print_connections()
print(" ")
nodes[1].print_connections()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)