Python3 运算符重载-随记

什么是运算符重载

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

作用:
    让自定义的实例像内建对象一样进行运算符操作
    让程序简洁易读
    对自定义对象将运算符赋予新的规则

算术运算符的重载:

    方法名                          运算符和表达式       说明
    __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                幂运算

 

操作试例:

class myClass1:
    def __init__(self,a,b):
        self.a=a
        self.b=b
    def __str__(self):
        return "myClass1 (%d,%d)" % (self.a,self.b)
    def __add__(self, other):
        return myClass1 (self.a+other.a,self.b+other.b)

s=myClass1(7,2)
s1=myClass1(2,6)
print(s+s1)


运行结果:
myClass1 (9,8)

反向运算符的重载

       当运算符的左侧为内建类型时,右侧为自定义类型进行算术匀算符运算时会出现TypeError错误,因为无法修改内建类型的代码实现运算符重载,此时需要使用反向运算符的重载
反向算术运算符的重载:
    方法名                             运算符和表达式     说明
    __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,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       不等于
        

  • 位运算符重载

    方法名                  运算符和表达式        说明
    __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            右移

  • 反向位运算符重载

    方法名                  运算符和表达式       说明
    __and__(self,lhs)       lhs & rhs            位与
    __or__(self,lhs)        lhs | rhs            位或
    __xor__(self,lhs)       lhs ^ rhs            位异或
    __lshift__(self,lhs)    lhs <<rhs            左移
    __rshift__(self,lhs)    lhs >>rhs            右移

  • 复合赋值位相关运算符重载

    方法名                   运算符和表达式        说明
    __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            右移

  • 一元运算符的重载

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

  • in /not in 运算符重载

格式:
    def __contains__(self,e):
        语句
注: in / not in 返回布尔值 True / False
当重载了__contains__后,in和not in运算符都可用
not in 运算符的返回值与 in相反

 

  • 索引和切片运算符重载方法:

    方法名                  运算符和表达式       说明
    __getitem__(self,i)     x = self(i)          索引/切片取值
    __setitem__(self,i,v)   self[i] = v          索引/切片赋值
    __delitem__(self,i)     del self[i]          del语句删除索引/切片
 作用: 让自定义的类型的对象能够支持索引和切片操作

切片方法类型slice构造函数

    作用:
           用于创建一个slice对象,此对于用于切片操作的传值
    格式:
           slice(start = None,stop = None ,step = None)
    slice对象的实例属性:
        start  切片的起始值,默认为None
        stop   切片的终止值,默认为None
        step   切片的步长,默认为None

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章