Python---运算符重载

什么是运算符重载:

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

算数运算符重载:

方法名                             运算符和表达式            说明
            __add__(self,other)        self + other                   加法
            __sub__(self,other)        self - other                    减法
            __mul__(self,other)        self * other                    乘法
            __truediv__(self,other)   self / other                    除法
            __floordiv__(self,other)  self // other                   地板除
            __mod__(self,other)       self % other                 取模(求余)
            __pow__(self,other)       self **other                   幂运算

class Person:
    def __init__(self,num):
        self.num = num
    def __add__(self, other):
        return Person(self.num + other.num)
    def __sub__(self, other):
        return Person(self.num - other.num)
    def __str__(self):
        return "num = "+str(self.num)

p1 = Person(12)
p2 = Person(13)
print(p1+p2)
print(p1-p2)



F:\学习代码\Python代码\venv\Scripts\python.exe F:/学习代码/Python代码/day5/运算符重载2.py
num = 25
num = -1

Process finished with exit code 0

反向算数运算符重载:

            方法名                              运算符和表达式       说明
            __radd__(self,other)        other + self               加法
            __rsub__(self,other)        other - self                减法
            __rmul__(self,other)        other * self                乘法
            __rtruediv__(self,other)   other / self                除法
            __rfloordiv__(self,other)  other // self               地板除
            __rmod__(self,other)       other % self             取模(求余)
            __rpow__(self,other)       other ** self              幂运算

class Person:
    def __init__(self,num):
        self.num = num
    def __repr__(self):
        return "num = " + repr(self.num)
    def __add__(self, other):
        print("__add__")
        return Person(self.num + other)
    def __radd__(self, other):
        print("__radd__")
        return Person(self.num + other)
    def __sub__(self, other):
        print("__sub__")
        return Person(self.num - other)
    def __rsub__(self, other):
        print("__rsub__")
        return Person(self.num - other)



p1 = Person(10)
p2 = Person(20)
p3 = 20 - p1
print(p3)
p4 = p1 - 20
print(p4)
p5 = 10 + p2
print(p5)
p6 = p2 + 10
print(p6)




F:\学习代码\Python代码\venv\Scripts\python.exe F:/学习代码/Python代码/day5/反向运算符重载.py
__rsub__
num = -10
__sub__
num = -10
__radd__
num = 30
__add__
num = 30

Process finished with exit code 0

 复合赋值算术运算符的重载:
        方法名                             运算符和表达式      说明
        __iadd__(self,other)        self += other           加法
        __isub__(self,other)        self -= other            减法
        __imul__(self,other)        self *= other            乘法
        __itruediv__(self,other)   self /= other            除法
        __ifloordiv__(self,other)  self //=other            地板除
        __imod__(self,other)       self %= other         取模(求余)
        __ipow__(self,other)       self **=other           幂运算

class Person:
    def __init__(self,num):
        self.num = num
    def __repr__(self):
        return "num = "+ repr(self.num)
    def __iadd__(self, other):
        print("__iadd__")
        self.num += other.num
        return self
    def __add__(self, other):
        print("__add__")
        return Person(self.num + other)

if __name__ == '__main__':
    p1 = Person(10)
    p2 = Person(20)
    p3 = p1 + 10
    print(p3)
    p2 += p1
    print(p2)



F:\学习代码\Python代码\venv\Scripts\python.exe F:/学习代码/Python代码/day5/复合算数运算符重载.py
__add__
num = 20
__iadd__
num = 30

Process finished with exit code 0

比较算数运算符:

方法名                     运算符和表达式         说明
__lt__(self, other)            self < other                             小于
__le__(self, other)           self <= other                           小于等于
__gt__(self, other)           self > other           大于
__ge__(self, other)          self >= other           大于等于
__eq__(self, other)          self == other           等于
__ne__(self, other)          self != other            不等于

位运算符重载
方法名                        运算符和表达式                          说明
__invert__(self)                     ~ self                                           取反(一元运算符)
__and__(self, other)             self & other                                 位与
__or__(self, other)                self | other                                   位或
__xor__(self, other)              self ^ other                                  位异或
__lshift__(self, other)            self << other                                左移
__rshift__(self, other)           self >> other                                右移

反向位运算符重载
方法名                        运算符和表达式        说明
__rand__(self, other)                           other& self             位与
__ror__(self, other)                              other| self               位或
__rxor__(self, other)                            other^ self                 位异或
__rlshift__(self, other)                          other<< self            左移
__rrshift__(self, other)                         other>> self            右移

复合赋值位运算符重载
方法名                       运算符和表达式        说明
__iand__(self, other)                self &= other             位与
__ior__(self, other)                   self |= other              位或
__ixor__(self, other)                 self ^= other                             位异或
__ilshift__(self, other)              self <<= other                          左移
__irshift__(self, other)                self >>= other          右移

猜你喜欢

转载自blog.csdn.net/qq_41112887/article/details/88758219
今日推荐