python运算符重载

发布时间 2023-06-14 09:38:20作者: wancy

1. 重载的概念

  重载一般指函数重载。是在一个类里面,函数名字相同,而参数不同。由于python参数没有类型,所以在python中,是没有函数重载的。比如下面这样运行后直接报错。写多个同名的函数,只有最后一个生效。

 

2. 运算符重载

  运算符重载指的是将运算符与类方法关联起来,每个运算符对应一个指定的内置方法。python通过重写这些内置的方法,实现运算符的重载功能。

3. __str__重载

  当我们调用print()函数打印对象时,其实是调用了对象的__str__()函数。

class Vector(object):
   def __init__(self, x=None, y=None):
       self.x=x
       self.y=y
   def __str__(self):
       return f"Vector{self.x,self.y}"

v1=Vector(1,1)
print(v1)

 

 4. 加法运算重载

  加法重载为__add__函数或__iadd__函数,两者有区别。对于不可变对象,比如int变量,元组等,只有__add__函数可以重载。没有__iadd__函数。对于可变对象,如类对象,列表等有__add__函数与__iadd__函数。__add__返回新对象,__iadd__返回旧对象。对于可变对象什么时候调用__add__,又什么时候调用__iadd__看下面例子。

 

class Vector(object):
    def __init__(self, x=None, y=None):
        self.x=x
        self.y=y

    #返回新对象
    def __add__(self, other):
        print("调用了_add__")
        #函数可以根据传入的参数类型,决定行为
        if type(other)==Vector:
            x=self.x+other.x
            y=self.y+other.y
        else:
            x=self.x+other
            y=self.y+other
        return Vector(x,y)

    #返回旧对象
    def __iadd__(self, other):
        print("调用了__iadd__")
        self.x+=other.x
        self.y+=other.y
        return self

    def __str__(self):
        return f"Vector{self.x,self.y}"

v1=Vector(1,1)
v2=Vector(1,2)
v3=v1+v2
print(v3)

v4=Vector(0,0)
print("v4的id=",id(v4))
v4+=Vector(0,1)
print("v4的id=",id(v4))
print(v4)

v5=Vector(1,0)
print("v5的id=",id(v5))
v5=v5+Vector(2,2)
print("v5的id=",id(v5))
print(v5)

 

  可以发现使用v4+=Vector(0,1)v4对象的内存地址没有发生变化,因为调用的是__iadd__。而v5=v5+Vector(2,2)内存地址发生了变化,因为调用了__add__,生成了一个新的对象。

5. ==重载

  使用__eq__函数重载==

class Vector(object):
    def __init__(self, x=None, y=None):
        self.x=x
        self.y=ydef __str__(self):
        return f"Vector({self.x,self.y})"

    def __eq__(self, other):
        #默认:按地址比较
        #return id(self)==id(other)
        #重写:比较内容
        return self.__dict__==other.__dict__

v1=Vector(1,2)
v2=Vector(1,2)
print(v1==v2)#
print(v1.__eq__(v2))#

  我们希望,如果对象的成员变量值完全相同,认为两者相等,重写__eq__时,使用return self.__dict__==other.__dict__判断。

 

6. 大于重载

 

  使用__gt__函数,往往用于比较两个对象或是对对象进行排序。

 

class Vector(object):
    def __init__(self, x=None, y=None):
        self.x=x
        self.y=y
def __str__(self):
        return f"Vector({self.x,self.y})"

    def __gt__(self, other)->bool:
        return self.x>other.x

list01=[Vector(1,2),Vector(3,2),Vector(0,1)]
print(max(list01))
print("排序前",end=" ")
for item in list01:
    print(item,end=" ")
print()
list01.sort()#升序
list01.sort(reverse=True)#逆序
print("排序后",end=" ")
for item in list01:
    print(item,end=" ")

 

7. 减法重载

  使用__sub__函数与__isub__函数

class Vector(object):
   def __init__(self, x=None, y=None):
       self.x=x
       self.y=y

def __str__(self): return f"Vector{self.x,self.y}" def __sub__(self, other): x=self.x-other.x y=self.y-other.y return Vector(x,y) def __isub__(self, other): self.x -= other.x self.y -= other.y return self v1=Vector(1,2) print(id(v1)) v1-=Vector(1,1) print(v1) print(id(v1))

 小结:除了以上介绍的之外,还有很多,可以参考文章最后的链接,讲解的比较详细。

 

参考资料:

https://blog.csdn.net/weixin_42272768/article/details/124443158

https://blog.csdn.net/XQC_KKK/article/details/121889641

  若存在不足或错误之处,欢迎指正与评论!