将类转换为Python列表

3
我可以帮您将简单的 [0, 20, 0, 20, 0, 20] 转换为一个列表代码:
geo = hou.pwd().geometry()

print geo.boundingBox()
print type(geo.boundingBox())

输出:

[0, 20, 0, 20, 0, 20]
<class 'hou.BoundingBox'>

测试:

list(geo.boundingBox())
len(geo.boundingBox())

I get the following error:

object BoundingBox is not iterable.
Object of type 'BoundingBox' has no len()

我阅读了所有相关的问题,但它们似乎不能解决我的问题。

3
类可以重写__str__()和__repr__()方法以显示任何它们想要的内容。仅仅因为它们看起来像列表并不意味着它们就是列表。 - President James K. Polk
hou.BoundingBox 的属性是什么?你能够通过调用 len(geo.boundingBox())(或类似方式)来获取它的大小吗?你能够对其进行索引 (geo.boundingBox()[1]) 吗? - inspectorG4dget
@inspectorG4dget,我添加了你的问题。 - Rosenthal
1
@Neo:如果你的意思是它们返回 BoundingBox,那么不,它们不会。如果你的意思是它们返回相同的类型,我不明白为什么这很重要。 - user2357112
2
是的,但它们很容易转换为列表,因为它们支持__len____getitem__。Ignacio的答案向您展示了如何操作。 - user2357112
显示剩余5条评论
1个回答

3

Vector3可以转换为list,因为它们实现了项目访问方法。

list(geo.boundingBox().minvec()) + list(geo.boundingBox().maxvec())

这样不行,你必须先调用boundingbox函数。 - Rosenthal
geo.boundingBox().maxvec() - Rosenthal
你说的"Item access methods"是指可迭代对象吗? - Rosenthal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接