我有以下代码:
class VectorN(object):
def __init__(self, param):
if isinstance(param, int):
self.dim = param
self.data = [0.0] * param
elif isinstance(param, tuple):
self.dim = 3
self.data = param
#else:
#raise TypeError("You must pass an int or sequence!")
def __str__(self):
return "<Vector" + str(self.dim) + ": " + str(self.data) + ">"
def __len__(self):
return len(self.data)
def __setitem__(self, key, item):
self.data[key] = item
现在,当我尝试__setitem__
使用以下代码调用该方法时,
w = VectorN((1.2, "3", 5))
w.setitem(0, 9.9)
print(z)
print(w)
print(z[0])
print(len(v))
它给了我:
AttributeError:“VectorN”对象没有属性“setitem”