[英]“for item in list” - where does the None item come from?
i got this list of objects with one item in it. 我得到了其中包含一项的对象列表。
print self.parameters
print len(self.parameters)
for p in self.parameters:
print p
when i print out the list and len of the lsit i see the expected: one item. 当我打印出列表和镜头时,我看到了预期的:一项。 But when looping over the lsit i also get a None item...!?
但是当循环访问lsit时,我也得到了None项目...!?
[<__main__.Parameter object at 0x00000000022D4828>]
1
<__main__.Parameter object at 0x00000000022D4828>
None
what is going on here? 这里发生了什么? (yes im sure, that the "None" output is from this print statement)
(是的,请确保“无”输出来自此打印语句)
EDIT: i was manipulating the list i was looping over: 编辑:我正在操纵我遍历的列表:
print self.parameters
print len(self.parameters)
for p in self.parameters:
print p
(...)
self.parameters.append(<something that returned None>)
Don't do this. 不要这样
for p in self.parameters:
print p
...
self.parameters.append(...) # No.
See Modifying list while iterating -- basically, you shouldn't modify something while iterating over it. 请参阅在迭代时修改列表 -基本上, 在迭代时不应修改某些内容。 You can make a copy if you want:
您可以根据需要进行复制:
for p in list(self.parameters):
print p
...
self.parameters.append(...) # Okay
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.