[英]Unexpected None in python print statement
I'm trying to print out a list of nodes connected to other nodes so I can view them for debugging some python code revolving around graphs.我正在尝试打印出连接到其他节点的节点列表,以便我可以查看它们以调试一些围绕图形的 Python 代码。 I'm doing this for practice so I'm setting up nodes/edges/groups myself.
我这样做是为了练习,所以我自己设置节点/边/组。
I've tried printing out the connections with my function inside of the myNode class using a for each type loop.我尝试使用 for each type 循环打印出与 myNode 类中的函数的连接。 But I get different output if I specify the index of the array and call the same method on it.
但是如果我指定数组的索引并对其调用相同的方法,我会得到不同的输出。
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())
The for/each has good output, or appears to be good: for/each 有很好的输出,或者看起来很好:
2 5 3 0
which differs from the line with the indexed print:这与带有索引打印的行不同:
2 5 3 0 None
Is this expected behavior in python?这是python中的预期行为吗? How can I get around this?
我怎样才能解决这个问题?
your print_connections(self)
doesn't return any value, thus return None
, and what you're trying to do is with print(nodes[1].print_connections())
is to print the returned value by nodes[1].print_connections()
, which is going to be None
, so what you should do instead is just,您的
print_connections(self)
不返回任何值,因此返回None
,您要做的是使用print(nodes[1].print_connections())
打印由nodes[1].print_connections()
返回的值nodes[1].print_connections()
,这将是None
,所以你应该做的只是,
for node in nodes:
node.print_connections()
print(" ")
nodes[1].print_connections()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.