运算符重载

运算符重载
  什么是运算符重载
    让自定义的类生成的对象(实例)能够使用运算符进行操作

作用:
  1. 让自定义的类的实例能够运行运算符操作
  2. 让程序简洁易读
  3. 对自定义对象将运算符赋序新的运算规则
说明:
  运算符重载方法的参数已经有固定的含义,不建议改变原有的意义

二元运算符的重载方法模式:
  def __xxx__(self, other):
    语句块

算术运算符重载
       方法名               运算符和表达式   说明
 __add__(self, rhs)         self +  rhs    加法
 __sub__(self, rhs)         self -  rhs    减法
 __mul__(self, rhs)         self *  rhs    乘法
 __truediv__(self, rhs)     self /  rhs    除法
 __floordiv__(self, rhs)    self // rhs    地板除
 __mod__(self, rhs)         self %  rhs    取模(求余)
 __pow__(self, rhs)         self ** rhs    幂
反向算术运算符重载
       方法名               运算符和表达式   说明
 __radd__(self, lhs)        lhs +  self    加法
 __rsub__(self, lhs)        lhs -  self    减法
 __rmul__(self, lhs)        lhs *  self    乘法
 __rtruediv__(self, lhs)    lhs /  self    除法
 __rfloordiv__(self, lhs)   lhs // self    地板除
 __rmod__(self, lhs)        lhs %  self    取模(求余)
 __rpow__(self, lhs)        lhs ** self    幂

复合赋值算术运算符的重载
  以复合赋值算术运算符 x += y 为例,此运算会优先调用x.__iadd__(y) 方法,
  如果没有__iadd__方法时会将复合赋值运算拆解为 x = x + y,然后调用x = x.__add__(y) 方法
  如果再不存在 __add__方法则会触发TypeError异常

这样的话,__iadd__的话,self的id是不会改变的,__add__ 的话,self就会改变id

复合赋值算术运算符重载
       方法名               运算符和表达式   说明
 __iadd__(self, rhs)        self +=  rhs    加法
 __isub__(self, rhs)        self -=  rhs    减法
 __imul__(self, rhs)        self *=  rhs    乘法
 __itruediv__(self, rhs)    self /=  rhs    除法
 __ifloordiv__(self, rhs)   self //= rhs    地板除
 __imod__(self, rhs)        self %=  rhs    取模(求余)
 __ipow__(self, rhs)        self **= rhs    幂
比较运算符的重载
       方法名               运算符和表达式   说明
 __lt__(self, rhs)        self <   rhs    小于
 __le__(self, rhs)        self <=  rhs    小于等于
 __gt__(self, rhs)        self >   rhs    大于
 __ge__(self, rhs)        self >=  rhs    大于等于
 __eq__(self, rhs)        self ==  rhs    等于
 __ne__(self, rhs)        self !=  rhs    不等于

注:比较运算符通常放回布尔值True 或 False
位运算符重载
       方法名               运算符和表达式   说明
 __invert__(self)          ~ self      取反(一元运算符)
 __and__(self, rhs)        self &  rhs    位与
 __or__(self, rhs)         self |  rhs    位或
 __xor__(self, rhs)        self ^  rhs    位异或
 __lshift__(self, rhs)     self << rhs    左移
 __rshift__(self, rhs)     self >> rhs    右移
反向位运算符重载
       方法名               运算符和表达式   说明
 __rand__(self, lhs)        lhs &  self    位与
 __ror__(self, lhs)         lhs |  self    位或
 __rxor__(self, lhs)        lhs ^  self    位异或
 __rlshift__(self, lhs)     lhs << self    左移
 __rrshift__(self, lhs)     lhs >> self    右移
复合赋值位运算符重载
       方法名               运算符和表达式   说明
 __iand__(self, rhs)        self &=  rhs    位与
 __ior__(self, rhs)         self |=  rhs    位或
 __ixor__(self, rhs)        self ^=  rhs    位异或
 __ilshift__(self, rhs)     self <<= rhs    左移
 __irshift__(self, rhs)     self >>= rhs    右移

--------------------------------------------------------------------------------------------------------------------

一元运算符重载
       方法名               运算符和表达式   说明
 __invert__(self)          ~ self      取反(一元运算符)
 __pos__(self)             + self      正号
 __neg__(self)             - self      负号

语法:
   def __xxx__(self):
       ...


in / not in 运算符重载
重载方法:
  def __contains__(self, e):
      语句

    注:in / not in放回布尔值 True/False
    当重载了__contains__后,in和not in 运算符都可用,not in 运算符的返回值与in相反


示例代码:
    def __contains__(self, e):
#       return e in self.data
        return True if e in self.data else False       #这两个都可以使用


if 9 in L1:
    print("9在L1内")
else:
    print("9不在L1内")

输出结果:
9不在L1内
原文地址:https://www.cnblogs.com/zengsf/p/9557425.html