# 实现向量类型
class Vector:

def __init__(self, x=0, y=0):
self.x = x
self.y = y

class Vector:

def __init__(self, x=0, y=0):
self.x = x
self.y = y
# 实现向量加法
x = self.x + other.x
y = self.y + other.y
return Vector(x, y)

class Vector:

def __init__(self, x=0, y=0):
self.x = x
self.y = y
# 实现向量加法
x = self.x + other.x
y = self.y + other.y
return Vector(x, y)

# 实现向量乘法，例如r*3
def __mul__(self, scalar):
return Vector(self.x*scalar, self.y*scalar)

class Vector:

def __init__(self, x=0, y=0):
self.x = x
self.y = y

# 真假值，如果向量模为0，返回false
def __bool__(self):
return bool(abs(self))
# 实现向量加法
x = self.x + other.x
y = self.y + other.y
return Vector(x, y)

# 实现向量乘法，例如r*3
def __mul__(self, scalar):
return Vector(self.x*scalar, self.y*scalar)

# 返回向量的模
# hypot()返回欧几里德范数 sqrt(x*x + y*y)
def __abs__(self):
return hypot(self.x, self.y)

v = Vector(2, 3)
print(v)
v2 = Vector(4, 5)
print(v+v2)
print(v+v2*2)
<__main__.Vector object at 0x000002B4B1843C50>
<__main__.Vector object at 0x000002B4B1843EF0>
<__main__.Vector object at 0x000002B4B1843898>

class Vector:

def __init__(self, x=0, y=0):
self.x = x
self.y = y

# 真假值，如果向量模为0，返回false
def __bool__(self):
return bool(abs(self))
# 实现向量加法
x = self.x + other.x
y = self.y + other.y
return Vector(x, y)

# 实现向量乘法，例如r*3
def __mul__(self, scalar):
return Vector(self.x*scalar, self.y*scalar)

# 返回向量的模
# hypot()返回欧几里德范数 sqrt(x*x + y*y)
def __abs__(self):
return hypot(self.x, self.y)

# 实现__repr__方法，在控制台打印向量时会输出Vector(1, 2)
# 实现__str__，使用str()返回字符串
def __repr__(self):
return 'Vector(%r, %r)' % (self.x, self.y)

v = Vector(2, 3)
print(v)
v2 = Vector(4, 5)
print(v+v2)
print(v+v2*2)
print(abs(v))
Vector(2, 3)
Vector(6, 8)
Vector(10, 13)
3.605551275463989