我实现了一个示例类PointLocation
import collections as _collections
Point = _collections.namedtuple("Point", ("x", "y", "z"))
class PointLocation(object):
def __init__(self, x, y, z):
self._x = x
self._y = y
self._z = z
self._location = Point(x, y, z)
print "First Item: %s " % self._location[0]
def __repr__(self):
return "%s(%r, %r, %r)" % (
self.__class__.__name__,
self._x,
self._y,
self._z,
)
def __getitem__(self, key):
"""
to make the object to be used in a manner similar to a tuple or list
"""
return self._location.__getitem__(key)
def __setitem__(self, key, value):
if key == 0:
self._location = Point(self._location.x, value)
else:
self._location = Point(value, self._location.y)
我试图设置一个参数的值,使用:
pointLocationObj[0] = 1
但我总是犯错
Traceback (most recent call last):
File "/usr/san/Desktop/testScripts/classObject_returnsList.py", line 40, in <module>
pointLocationObj[0] = 7
File "/usr/san/Desktop/testScripts/classObject_returnsList.py", line 32, in __setitem__
self._location = Point(self._location.x, value)
TypeError: __new__() takes exactly 4 arguments (3 given)
您的
Point
命名元组需要x
、y
和z
参数:虽然您只提供了其中的两个;但您可能也想传入
self._location.z
:还可以使用^{} method 替换特定属性(返回新实例):
如果要使用索引引用
x
、y
和z
,请创建一个字典作为namedtuple._replace()
的关键字参数:这里我假设你的意思是
0
是x
,而不是1
。你知道吗只需更新下面的方法,就可以按预期工作。谢谢@Martijin帮我睁开眼睛。你知道吗
第二次更新:
相关问题 更多 >
编程相关推荐