西宁网站建设官网,WordPress理财模板,北京网页设计模板,爱网站找不到了4.10.4 运算篇
Python的类型系统是鸭子类型#xff0c;也就是不检查某个具体的对象是什么类型#xff0c;而是检查这个对象有没有相应的功能。在Python中有大量的魔术方法#xff0c;我们可以通过魔术方法的方式为对象添加相应的功能。 下面介绍Python中和运算相关的魔术方…4.10.4 运算篇
Python的类型系统是鸭子类型也就是不检查某个具体的对象是什么类型而是检查这个对象有没有相应的功能。在Python中有大量的魔术方法我们可以通过魔术方法的方式为对象添加相应的功能。 下面介绍Python中和运算相关的魔术方法。这些魔术方法是对两个对象在进行符号运算时对相应的运算符号进行重载从而实现相应的效果。 在本篇所讲的运算相关的魔术方法中参数other并不一定要和self一致。 很多时候我们用魔术方法只是借用了这个符号并不一定要符号该符号原来的定义。比如我们可以将左移符号重载成C中的cout。
4.10.4.1 add
addobj1 obj2
from icecream import icclass Vector:def __init__(self, x, y):self.x xself.y ydef __repr__(self):return fVertor({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(24) 4.10.4.2 sub
subobj1 – obj2
4.10.4.3 mul、matmul、pow
mulobj1 * obj2 matmulobj1 obj2 powobj1 ** obj2、pow(obj1 obj2 modNone) (obj1 ** obj2) % mod
4.10.4.4 truediv、floordiv、mod、divmod
truedivobj1 / obj2 floordivobj1 // obj2 modobj1 % obj2 divmoddivmod(obj1obj2)
4.10.4.5 lshift、rshift
lshiftobj1 obj2 rshiftobj1 obj2
4.10.4.6 and、xor、or
andobj1 obj2 xorobj1 ^ obj2 orobj1 | obj2
4.10.4.7 运算符的r版本
如下代码所示当我们自定义乘法判断相乘的对象是int类型时则将xy同时和int相乘并返回。但是如果是int类型数据和我们自定义的Vector类型相乘时则会报错因为是Python内置的int类型是不知道要如何和我们自定义的Vector类型相乘的。
from icecream import icclass Vector:def __init__(self, x, y):self.x xself.y ydef __repr__(self):return fVerctor({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(46) 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 fVerctor({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(46) ic| 2 * v1: Verctor(46) 这样一来如果操作符左侧的这个数据结构没有定义这个操作应该怎么完成的话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 fVertor({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(24) ic| v1: Vertor(03) 我们看到v v2 时调用了__iadd__魔术方法。返回结果Vertor(03)。但其实我们不创建这个__iadd__魔术方法也可以。
from icecream import icclass Vector:def __init__(self, x, y):self.x xself.y ydef __repr__(self):return fVertor({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(24) ic| v1: Vertor(24) 在本节前面提到的魔术方法中所有使用符号触发的都有其对应的i版本。也就是、-、*…
4.10.4.9 neg、pos
这2个魔术方法对应的是在数据结构之前添加一个-和符号。 neg-obj posobj
from icecream import icclass Vector:def __init__(self, x, y):self.x xself.y ydef __repr__(self):return fVertor({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(-23) 4.10.4.10 abs、invert
absabs(obj) invert~obj # 位运算中的反转
4.10.4.11 complex、int、float
complexcomplexobj intintobj floatfloatobj 这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 fVertor({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
roundround(obj) # 四舍五入 truncmath.trunc(obj) # 无条件舍弃小数 floormath.floor(obj) # 向负无穷取整 ceilmath.ceil(obj) # 向正无穷取整