首页 > 其他 > 详细

运算符重载

时间:2020-02-10 21:27:56      阅读:67      评论:0      收藏:0      [点我收藏+]
  • 运算符重载

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

? ?

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

? ?

? ?

算术运算符的重载

?

运算符重载的方法的参数已经有了固定的含义,

不建议改变原有的运算符的含义及参数的意义。

? ?

二元运算符的重载方法格式:

# 类内的运算符重载

def __xx__(self, rhs):

????语句块

? ?

# 类外的运算符重载

def __xx__(lhs, rhs):

????语句块

lhs

left hand side

左边的操作数

rhs

right hand side

右边的操作数

? ?

方法名

运算符和表达式

说明

__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

幂运算

? ?

demo:

class Mynumber:

????def __init__(self,v):

????????self.data = v

? ?

????def __repr__(self): #消除两边的尖括号

????????return "Mynumber(%d)"%self.data

? ?

????def __add__(self,other):

????????‘‘‘此方法用来制定self + other的规则‘‘‘

????????v = self.data + other.data

????????return Mynumber(v) #v创建一个新的对象返回给调用者

????def __sub__(self,other):

????????‘‘‘此方法用来制定self - other的规则‘‘‘

????????v = self.data - other.data

????????return Mynumber(v)

? ?

n1 = Mynumber(100)

n2 = Mynumber(200)

# n3 = n1 + n2

n3 = n1+n2 # n3 = n1.__add__(n2)

print(n3)?? #Mynumber(300)

n4 = n3 - n2 #等同于n4 = n3.__sub__(n2)

print("n4 = ",n4)

? ?

反向运算符的重载

当运算符的左侧为内建类型,右侧为自定义类型时,进行算术运算符重载时会出现TypeError错误,因为无法修改内建类型的代码以实现运算符重载,此时需要使用反向运算符的重载。

反向运算符的重载相当于C++中用友元函数实现实现运算符重载。

__radd__(self, lhs)

# reverse_add,反向加法的重载

? ?

反向运算符的重载方法格式:

# 类内的运算符重载

def __rxx__(self, lhs):

????语句块

? ?

方法名

运算符和表达式

说明

__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

left shift,左移

__rshift__(self, rhs)

self >> rhs

right shift,右移

? ?

? ?

反向位运算符重载

方法名

运算符和表达式

说明

__and__(self, lhs)

lhs & rhs

位与

__or__(self, lhs)

lhs | rhs

位或

__xor__(self, lhs)

lhs ^ rhs

位异或

__lshift__(self, lhs)

lhs << rhs

left shift,左移

__rshift__(self, lhs)

lhs >>rhs

right shift,右移

? ?

? ?

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

方法名

运算符和表达式

说明

__iand__(self, rhs)

self & rhs

位与

__ior__(self, rhs)

self | rhs

位或

__ixor__(self, rhs)

self ^ rhs

位异或

__ilshift__(self, rhs)

self << rhs

left shift,左移

__irshift__(self, rhs)

self >> rhs

right shift,右移

? ?

? ?

一元运算符的重载

语法:

class 类名:

????def __xxx__(self):

????????pass

? ?

方法名

运算符和表达式

说明

__neg__(self)

- self

negative,负号

__pos__(self)

+ self

positive,正好

__invert__(self)

~ self

invert,取反

?

?

?

?

?

运算符重载

原文:https://www.cnblogs.com/audacious/p/12292089.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!