Python class Vector 的创建,pythonvector,创建一个向量的class


创建一个向量的class,可以让vector相加,相减, 乘以一个系数, 或者清空成为(0,0,0)

from math import sqrtclass Vector:    """    A Vector is a 3-tuple of (x,y,z) coordinates.    """    def __init__(self,x,y,z):        self._x = x        self._y = y        self._z = z    def __repr__(self):        return '{%.3g} ,{%.3g} ,{%.3g}'%(self._x, self._y, self._z)    def __str__(self):        return '({},{},{})'.format(self._x, self._y, self._z)    def __add__(self,other):        return Vector(self._x + other._x, self._y + other._y, self._z + other._z)    def __sub__(self,other):        return Vector(self._x - other._x, self._y - other._y, self._z - other._z)    def norm(self):        result = sqrt(self._x**2 + self._y**2 + self._z**2)        return result    def __mul__(self,other):        return Vector(self._x * other, self * other, self._z * other)    def x(self):        return self._x    def y(self):        return self._y    def z(self):        return self._z    def clear(self):        return Vector(0 ,0 ,0)

以下是我test的结果

v1 = Vector(1, 1, 1)v2 = Vector(2, 2, 2)v3 = Vector(3, 3, 3)assert str(v1) == '(1,1,1)'assert str(v2) == '(2,2,2)'assert Vector(1, 2, 3) == Vector(1, 2, 3)assert Vector(1, 2, 3) != Vector(1.001, 2.001, 3.001)---------------------------------------------------------------------------AssertionError                            Traceback (most recent call last)<ipython-input-4-158bfce1869b> in <module>()----> 1 assert Vector(1, 2, 3) == Vector(1, 2, 3)      2 assert Vector(1, 2, 3) != Vector(1.001, 2.001, 3.001)AssertionError: assert v1 + v2 == v3---------------------------------------------------------------------------AssertionError                            Traceback (most recent call last)<ipython-input-8-aee75f985c01> in <module>()----> 1 assert v1 + v2 == v3      AssertionError: assert v3 - v2 == v1---------------------------------------------------------------------------AssertionError                            Traceback (most recent call last)<ipython-input-6-b6adfd2202df> in <module>()----> 1 assert v3 - v2 == v1AssertionError: assert v3 == v1 * 3#Error 信息太多 不复制出来了assert round(v2.norm(), 10) == round(sqrt(12), 10)v1.clear()assert v1 == Vector(0,0,0)---------------------------------------------------------------------------AssertionError                            Traceback (most recent call last)<ipython-input-9-188a44181827> in <module>()      1 v1.clear()----> 2 assert v1 == Vector(0,0,0)AssertionError: 

问题到底出在哪里?
比如向量相加,我print v1+v2 是(3,3,3)v3 也是(3,3,3),为什么还是assertion error?

你需要定义 __eq__:

    def __eq__(self, a):        return self._x == a._x and self._y == a._y and self._z == a._z

原因在此:

The behavior of the default equality comparison, that instances with different identities are always unequal, may be in contrast to what types will need that have a sensible definition of object value and value-based equality.
via https://docs.python.org/3/ref...

也就是说,没定义 __eq__ 时,v1 == v2 相当于 id(v1) == id(v2),当然是 False 了。

编橙之家文章,

评论关闭