4.10.4 运算篇
Python的类型系统是鸭子类型,也就是不检查某个具体的对象是什么类型,而是检查这个对象有没有相应的功能。在Python中有大量的魔术方法,我们可以通过魔术方法的方式为对象添加相应的功能。
下面介绍Python中和运算相关的魔术方法。这些魔术方法是对两个对象在进行符号运算时对相应的运算符号进行重载,从而实现相应的效果。
在本篇所讲的运算相关的魔术方法中,参数other并不一定要和self一致。
很多时候我们用魔术方法,只是借用了这个符号,并不一定要符号该符号原来的定义。比如:我们可以将<<
左移符号重载成C++中的cout。
4.10.4.1 add
add:obj1 + obj2
from icecream import icclass Vector:def __init__(self, x, y):self.x = xself.y = ydef __repr__(self):return f'Vertor({self.x},{self.y})'def __add__(self, other):return Vector(self.x + other.x, self.y + other.y)v1 = Vector(0, 1)
v2 = Vector(2, 3)
print(v1 + v2)
Vertor(2,4)
4.10.4.2 sub
sub:obj1 – obj2
4.10.4.3 mul、matmul、pow
mul:obj1 * obj2
matmul:obj1 @ obj2
pow:obj1 ** obj2、pow(obj1, obj2, mod=None) (obj1 ** obj2) % mod
4.10.4.4 truediv、floordiv、mod、divmod
truediv:obj1 / obj2
floordiv:obj1 // obj2
mod:obj1 % obj2
divmod:divmod(obj1,obj2)
4.10.4.5 lshift、rshift
lshift:obj1 << obj2
rshift:obj1 >> obj2
4.10.4.6 and、xor、or
and:obj1 & obj2
xor:obj1 ^ obj2
or:obj1 | obj2
4.10.4.7 运算符的r版本
如下代码所示,当我们自定义乘法,判断相乘的对象是int类型时,则将x,y同时和int相乘并返回。但是,如果是int类型数据和我们自定义的Vector类型相乘时,则会报错,因为是Python内置的int类型是不知道要如何和我们自定义的Vector类型相乘的。
from icecream import icclass Vector:def __init__(self, x, y):self.x = xself.y = ydef __repr__(self):return f'Verctor({self.x},{self.y})'def __mul__(self, other):# v1 * v2if isinstance(other, int):return Vector(self.x * other, self.y * other)v1 = Vector(2, 3)
ic(v1 * 2) # 本质:v1.__mul__(2)
ic(2 * v1)
ic| v1 * 2: Verctor(4,6)
Traceback (most recent call last):
File “E:\BaiduSyncdisk\FrbPythonFiles\t2.py”, line 20, in
ic(2 * v1)
TypeError: unsupported operand type(s) for *: ‘int’ and ‘Vector’
解决办法是定义一个__rmul__魔法方法,如下:
from icecream import icclass Vector:def __init__(self, x, y):self.x = xself.y = ydef __repr__(self):return f'Verctor({self.x},{self.y})'def __mul__(self, other):# v1 * v2if isinstance(other, int):return Vector(self.x * other, self.y * other)def __rmul__(self, other):# v1 * v2if isinstance(other, int):return Vector(self.x * other, self.y * other)v1 = Vector(2, 3)
ic(v1 * 2)
ic(2 * v1)
ic| v1 * 2: Verctor(4,6)
ic| 2 * v1: Verctor(4,6)
这样一来,如果操作符左侧的这个数据结构没有定义这个操作应该怎么完成的话,Python就会尝试去找操作符右侧这个对象的操作符的r版本魔法方法。这样就可以正常完成int * Verctor的操作了。
这里,之前列出的所有和数的计算有关的魔法方法都有其r版本。
4.10.4.8 运算符的i版本
i版本运算符魔法方法最后是修改self并返回。其实对应的符号就是操作符=
。比如:v1 += v2就会调用:v1.iadd(v2)。
from icecream import icclass Vector:def __init__(self, x, y):self.x = xself.y = ydef __repr__(self):return f'Vertor({self.x},{self.y})'def __add__(self, other):return Vector(self.x + other.x, self.y + other.y)def __iadd__(self, other):return Vector(self.x * other.x, self.y * other.y)v1 = Vector(0, 1)
v2 = Vector(2, 3)
ic(v1 + v2)
v1 += v2
ic(v1)
ic| v1 + v2: Vertor(2,4)
ic| v1: Vertor(0,3)
我们看到,v += v2 时调用了__iadd__魔术方法。返回结果:Vertor(0,3)。但其实,我们不创建这个__iadd__魔术方法也可以。
from icecream import icclass Vector:def __init__(self, x, y):self.x = xself.y = ydef __repr__(self):return f'Vertor({self.x},{self.y})'def __add__(self, other):return Vector(self.x + other.x, self.y + other.y)v1 = Vector(0, 1)
v2 = Vector(2, 3)
ic(v1 + v2)
v1 += v2
ic(v1)
ic| v1 + v2: Vertor(2,4)
ic| v1: Vertor(2,4)
在本节前面提到的魔术方法中,所有使用符号触发的都有其对应的i版本。也就是:+=、-=、*=…
4.10.4.9 neg、pos
这2个魔术方法对应的是在数据结构之前添加一个-
和+
符号。
neg:-obj
pos:+obj
from icecream import icclass Vector:def __init__(self, x, y):self.x = xself.y = ydef __repr__(self):return f'Vertor({self.x},{self.y})'def __neg__(self):return Vector(-self.x, -self.y)def __pos__(self):return Vector(self.x, self.y)v1 = Vector(-2, 3)
ic(-v1)
ic(+v1)
ic| -v1: Vertor(2,-3)
ic| +v1: Vertor(-2,3)
4.10.4.10 abs、invert
abs:abs(obj)
invert:~obj # 位运算中的反转
4.10.4.11 complex、int、float
complex:complex(obj)
int:int(obj)
float:float(obj)
这3个魔术方法规定返回的结果必须是其对应的数据结构。
4.10.4.12 index
index:代表当你把这个数据结构当成index使用的时候等价于什么。
如果我们定义了这个__index__,而没有定义__ complex__、int、__float__方法的话,那么complex、int、float都会默认使用这个__index__魔法方法。
from icecream import icclass Vector:def __init__(self, x, y):self.x = xself.y = ydef __repr__(self):return f'Vertor({self.x},{self.y})'def __index__(self):return int(self.x)v1 = Vector(2, 3)
lst = list('Python')
ic(lst[v1]) # [v1]返回2,相当于:lst[int(self.x)]
ic| lst[v1]: ‘t’
4.10.4.13 round、trunc、floor、ceil
round:round(obj) # 四舍五入
trunc:math.trunc(obj) # 无条件舍弃小数
floor:math.floor(obj) # 向负无穷取整
ceil:math.ceil(obj) # 向正无穷取整