【python进阶】模拟数值类型

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_36372879/article/details/86592241

在这里插入图片描述

from math import hypot
class Vector:
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y

    def __repr__(self):   #定义输出表示方法
        return 'Vector(%r, %r)' % (self.x, self.y)

    def __abs__(self):
        return hypot(self.x, self.y)    #求向量的模

    def __bool__(self):
        return bool(abs(self))

    def __add__(self, other):   #定义+方法
        x = self.x + other.x
        y = self.y + other.y
        return Vector(x, y)

    def __mul__(self, other):   #定义 * 方法
        return Vector(self.x * other, self.y * other)

v1 = Vector(2,4)
v2 = Vector(2,1)
print(v1 + v2)
v = Vector(3, 4)
print(abs(v))
print(abs(v * 3))

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_36372879/article/details/86592241