【发布时间】:2015-09-10 13:21:31
【问题描述】:
我有一个对象scene,它是Scene 类的一个实例,并且有一个children 列表,它返回:
[<pythreejs.pythreejs.Mesh object at 0x000000002E836A90>, <pythreejs.pythreejs.SurfaceGrid object at 0x000000002DBF9F60>, <pythreejs.pythreejs.Mesh object at 0x000000002E8362E8>, <pythreejs.pythreejs.AmbientLight object at 0x000000002E8366D8>, <pythreejs.pythreejs.DirectionalLight object at 0x000000002E836630>]
如果我想用 point 更新此列表,其类型为:
<class 'pythreejs.pythreejs.Mesh'>
我需要执行:
scene.children = list(scene.children) + [point]
通常,我会执行:
scene.children.append(point)
然而,虽然这两种方法都附加了point,但只有第一种方法实际更新了列表并产生了预期的输出(即网格上的体素)。为什么?
完整代码可以在here找到。
【问题讨论】:
-
不,
append应该可以工作。如果没有,那就是很奇怪的事情,你需要发minimal reproducible example。 -
我明白,不幸的是,我在生成一个发生此问题的最小示例时遇到了一些问题:(...目前试图弄清楚我是否在做一些愚蠢的事情导致这个问题
-
我能想到的唯一解释是
scene.children实际上不是一个列表,它是实现append的其他类,但其行为方式与列表不同。如果你把print(type(scene.children))放在scene.children.append(point)前面的那一行,输出是什么? -
@Kevin - 返回
<type 'list'>;注意scene是类Scene和children的实例是列表 -
您如何检查
scene.children的内容以查看其中是否存在point?您是在append行之后立即检查,还是稍后再检查?
标签: python list python-2.7 append ipython-notebook