AttributeError: 'Vector' object has no attribute 'plus' while adding new method in class Vector using python 2.7
AttributeError: 'Vector' object has no attribute 'plus' while adding new method in class Vector using python 2.7
我正在尝试向 python 2.7 中的 class Vector 添加新方法,
而 运行 程序,它 return 错误消息 python class 中创建的方法没有属性。
源代码如下:
class Vector(object):
def __init__(self, coordinates):
try:
if not coordinates:
raise ValueError
self.coordinates = tuple(coordinates)
self.dimension = len(coordinates)
except ValueError:
raise ValueError('The coordinates must be nonempty')
except TypeError:
raise TypeError('The coordinates must be an iterable')
def __str__(self):
return 'Vector: {}'.format(self.coordinates)
def __eq__(self, v):
return self.coordinates == v.coordinates
def plus(self, v):
new_coordinates = [x+y for x, y in zip(self.coordinates, v.coordinates)]
return Vector(new_coordinates)
myVector1 = Vector([8.218, -9.341])
myVector2 = Vector([-1.129, 2.111])
print myVector1.plus(myVector2)
output error message:
AttributeError: 'Vector' object has no attribute 'plus'
您没有正确限定函数的范围,方法必须在 class:
中
class Vector(object):
def __init__(self, coordinates):
try:
if not coordinates:
raise ValueError
self.coordinates = tuple(coordinates)
self.dimension = len(coordinates)
except ValueError:
raise ValueError('The coordinates must be nonempty')
except TypeError:
raise TypeError('The coordinates must be an iterable')
def __str__(self):
return 'Vector: {}'.format(self.coordinates)
def __eq__(self, v):
return self.coordinates == v.coordinates
def plus(self, v):
new_coordinates = [x + y for x,y in zip(self.coordinates, v.coordinates)]
return Vector(new_coordinates)
我正在尝试向 python 2.7 中的 class Vector 添加新方法, 而 运行 程序,它 return 错误消息 python class 中创建的方法没有属性。 源代码如下:
class Vector(object):
def __init__(self, coordinates):
try:
if not coordinates:
raise ValueError
self.coordinates = tuple(coordinates)
self.dimension = len(coordinates)
except ValueError:
raise ValueError('The coordinates must be nonempty')
except TypeError:
raise TypeError('The coordinates must be an iterable')
def __str__(self):
return 'Vector: {}'.format(self.coordinates)
def __eq__(self, v):
return self.coordinates == v.coordinates
def plus(self, v):
new_coordinates = [x+y for x, y in zip(self.coordinates, v.coordinates)]
return Vector(new_coordinates)
myVector1 = Vector([8.218, -9.341])
myVector2 = Vector([-1.129, 2.111])
print myVector1.plus(myVector2)
output error message:
AttributeError: 'Vector' object has no attribute 'plus'
您没有正确限定函数的范围,方法必须在 class:
中class Vector(object):
def __init__(self, coordinates):
try:
if not coordinates:
raise ValueError
self.coordinates = tuple(coordinates)
self.dimension = len(coordinates)
except ValueError:
raise ValueError('The coordinates must be nonempty')
except TypeError:
raise TypeError('The coordinates must be an iterable')
def __str__(self):
return 'Vector: {}'.format(self.coordinates)
def __eq__(self, v):
return self.coordinates == v.coordinates
def plus(self, v):
new_coordinates = [x + y for x,y in zip(self.coordinates, v.coordinates)]
return Vector(new_coordinates)