python_运算符重载

  • 什么是运算符重载
    让自定义的类生成的对象(实例)能够使用运算符进行操作
  • 作用:
    让自定义的类的实例像内建对象一样运行运算符操作
    让程序简洁易读
    对自定义的对象,将运算符赋予新的运算规则
  • 算数运算符的重载
__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		幂

注:rhs(right hands side) 右手边

示例:

#此实例示意加减乘法
class MyNumber:
	def __init__(self,value):
		self.data = value
	def __add__(self,rhs):
		return MyNumber(self.data+rhs.data)
	def __sub__(self,rhs):
		return MyNumber(self.data-rhs.data)
	def __mul__(self,rhs):
		return MyNumber(self.data*rhs.data)
	def __repr__(self):
		return '%d' % self.data

n1 = MyNumber(100)
n2 = MyNumber(200)
n3 = n1+n2
print(n3)			#300

练习:

#自定义列表类
class MyList:
	def __init__(self,iterable):
		self.data = list(iterable)
	def __repr__(self):
		return 'MyList(%r)' % self.data
	def __add__(self,rhs):
		L = self.data + rhs.data
		return MyList(L)
	def __mul__(self,v):
		return MyList(self.data*v)

L1 = MyList([1,2,3])
L2 = MyList([4,5,6])
L3 = L1+L2
print(L3)
L4 = L2+L1
print(L4)
L5 = L1*2
print(L5)

出现问题:无法实现 1 + obj
当对象出现在右手边时要实现反向运算符的重载

  • 反向运算符的重载
    当左手边的类型为内建类型,右手边为自定义类型时,要实现运算必须用以下方法重载
__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		幂

注:lhs(left hands side) 左手边  r-->reverse
  • 复合赋值算数运算符
__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	幂

注:rhs(right hands side) 右手边
  • 比较运算符的重载
__lt__(self,rhs)		self < rhs 			小于
__le__(self,rhs)		self <= rhs 		小于等于
__gt__(self,rhs)		self > rhs 			大于
__ge__(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	右移
  • 一元运算符的重载
__neg__(self)		-self		负号
__pos__(self)		+self		正号
__invert__(self)	~self		取反

示例:

#此示例示意列表的负号和正号方法重载
class MyList:
	def __init__(self,iterable):
		self.data = list(iterable)
	def __repr__(self):
		return 'MyList(%r)' % self.data
	def __neg__(self):
		L = (-x for x in self.data)	#相比于[]列表表达式
									#生成表达式不占据内存
		return MyList(L)
	def __pos__(self):
		L = (abs(x) for x in self.data)
		return MyList(L)

L = MyList([1,-2,3,-4])
L1 = -L
L2 = +L
print(L1)
print(L2)
  • 运算符重载说明:
    python规定运算符重载不能改变运算符的优先级
  • in / not in 运算符的重载
语法:
def __contains__(self,e)		e in self
说明:
in/not in 都只调用__contains__方法

示例:

class MyList:
	def __init__(self,iterable):
		self.data = list(iterable)
	def __contains__(self,e):
		for x in self.data:
			if e == x:
				return True
		return False
	
L = MyList([1,2,3,4])
if -1 in L:
	print('-1在L中')
else:
	print('-1不在L中')
if -1 not in L:
	print('-1不在L中')
  • 索引和切片运算符的重载
__getitem__(self,i)		x = self[i]  	索引/切片赋值
__setitem__(self,i,v)	self[i]=v		索引/切片赋值
__delitem__(self,i)		del self[i]		del语句删除索引等

作用:让自定义的类型和对象能够支持索引和切片操作

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值