首页 > 编程语言 >python 重载

python 重载

时间:2022-11-08 10:33:51浏览次数:36  
标签:__ Vector python self v1 other 重载 def

class Vector:
    def __init__(self,x=0.0, y=0.0, z=0.0):
        self.x = x
        self.y = y
        self.z = z
    def __str__(self):
        return 'Vector({0},{1},{2})'.format(self.x,self.y,self.z)

    def __neg__(self):
        return Vector(-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 __mul__(self, other):
        return Vector(other * self.x , other * self.y , other * self.z)

    def __rmul__(self, other):
        return Vector(other * self.x , other * self.y , other * self.z)



if __name__ == '__main__':
    v1 = Vector(1,2,3)
    v2 = Vector(4,5,6)
    print('-v1={}'.format(-v1))
    print('v1+v2={}'.format(v1+v2))
    print('v1-v2={}'.format(v1-v2))
    print('v1*2={}'.format(v1*2))
    print('2*v1={}'.format(2*v1))

标签:__,Vector,python,self,v1,other,重载,def
From: https://www.cnblogs.com/jiyiran/p/16868790.html

相关文章

  • python 使用docker开发
    背景pycharm+win10环境开发很多时候需要编译一些c++拓展例如bcrypt==3.1.4win安装一堆的MicrosoftVisualC++14.0isrequired使用docker可以模拟linu......
  • Python 读取图片的位置写入txt
    importossaveBasePath=r'E:\Mine_personnel'#存放txt文件的地址jpgfilepath=r'E:\Mine_personnel\images\val'#jpg图片的地址ftest=open(os.path.join(s......
  • python进阶(26)collections标准库
    前言这个模块实现了特定目标的容器,以提供Python标准内建容器dict,list,set,和tuple的替代选择。这个模块提供了以下几个函数函数作用namedtuple()创建命......
  • 【Python】Python使用Tk实现动态爱心效果
    【Python】Python使用Tk实现动态爱心效果是抄的一个UP主的(视频链接在文章结尾),自己改了点小东西,加了注释,然后最后光环的那里UP没有放出一部分代码,自己脑补了。写的时候发现......
  • 垃圾蜘蛛UA屏蔽封禁python|SM-G900P|OPPO A33|DataForSeoBot|omgili|SemrushBot|Adsbo
    (python|Python|SM-G900P|OPPOA33|DataForSeoBot|omgili|SemrushBot|Adsbot|AhrefsBot|mj12bot|MJ12|WebMeUp|serpstatbot|leiki|opensiteexplorer|hubspot)列表里面有两......
  • python课本学习-第一章
    chapter1python开发入门1、python之父:GuidovanRossum2、python语言的特征:简单易学免费&开源可移植性解释性面向对象在面向对象的语言中,程序是......
  • Python中变量的使用
    #函数学了#print()#id()#type()#快递,包裹变量=包裹#变量包括变量名变量值内存地址#变量的作用就是存储数据name='666'#print('helloworld')#......
  • python的四大基本数据结构
    list()列表用来装载不同数据类型的数据集结果列表的特点有序的可以装卸任意数据类型可以更改的如何表示list通过list()新建一个列表list('helloword')通过[]声......
  • 《Python程序设计——深入理解计算机系统的语言》上市了
    ​本书是为高校师生学习Python编程语言而设计编著的教材。全书分20章,其中包括:绪论;开发环境搭建;第一个Python程序;Python语法基础;数据类型;运算符;控制语句;数据结构;函数;面向对象......
  • python的六大基本类型
    通过type()函数来查看当前变量的数据类型int()整数float()浮点数-因为计算机内部只认识1和0,所以浮点数强调的时小数的表现形式string()字符串,字符序列-在有些语言中......