Magic Methods 2

Magic Methods 2: Mathematical Calculation P1

int, float, string, tuple, list… -> Factory Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
>>> type(len)
<class 'builtin_function_or_method'>
>>> type(dir)
<class 'builtin_function_or_method'>
>>> type(int)
<class 'type'> #类对象
>>> type(list)
<class 'type'>
>>> class C:
pass

>>> type(C)
<class 'type'>
>>> int('123')
123
>>> a = int('123')
>>> b = int('456')
>>> a + b
579
>>> # a = int('123') 实例化int的一个对象,返回实例化对象a, '123'为传入的参数
>>> # 对象可以进行计算!

An example of magic methods. In this example, we make the add and subtract reciprocated.

1
2
3
4
5
6
7
8
9
10
11
12
13
>>> class New_int(int):
def __add__(self, other):
return int.__sub__(self, other)
def __sub__(self, other):
return int.__add__(self, other)


>>> a = New_int(3)
>>> b = New_int(5)
>>> a + b
-2
>>> a - b
8

Some bugs we need to pay attention to! Otherwise we may come across infinite iteration.

1
2
3
4
5
6
7
8
9
10
11
>>> class Try_int(int):
def __add__(self, other):
return self + other
def __sub__(self, other):
return self - other


>>> a = Try_int(3)
>>> b = Try_int(5)
>>> a + b
>>> RecursionError: maximum recursion depth exceeded

The code can be fixed as below:

1
2
3
4
5
6
7
8
9
10
11
>>> class Try_int(int):
def __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)


>>> a = Try_int(3)
>>> b = Try_int(5)
>>> a + b
8

Here is the introduction of magic methods that is for calculation.

Calculation Operators

Magic Methods Function
__add__(self, other) ‘+‘
__sub__(self, other) ‘-‘
__mul__(self, other) ‘*‘
__truediv__(self, other) ‘/‘
__floordiv__(self, other) ‘//‘
__mod__(self, other) ‘%’
__divmod__(self, other) ‘divmod()’
__pow__(self, other[,modulo]) ‘**‘
__lshift__(self, other) ‘<<’
__rshift__(self, other) ‘>>’
__and__(self, other) ‘bit and: &‘
__xor__(self, other) ‘bit xor: ^‘
__or__(self, other) ‘bit or: |’

mod() 取模(余数)
divmod(a, b) -> (a//b, a%b)
1 byte = 8 bit
3 == 00000011 -> << -> 00000110

####reverse calculation

When the first cannot do the calculation, then let the latter do it. We need to know that self is the latter, other is the first, if we do the reverse calculation. Here the reverse magic methods is the ‘r’ + ‘magic name’, just as __radd__(self, other).

1
2
3
4
5
6
7
8
9
10
11
>>> class Nint(int):
def __radd__(self, other):
return int.__sub__(self, other)


>>> a = Nint('5')
>>> b = Nint('3')
>>> a + b
8
>>> 1 + b # -> b - 1 = 3 - 1 =2 (1 don't have 'add')
2

For those operations that attach importance to sequence. We need to be careful.

1
2
3
4
5
6
7
8
9
10
11
>>> class Nint(int):
def __rsub__(self, other):
return int.__sub__(other, self)
# Otherwise will be wrong.


>>> a = Nint('5')
>>> a - 3
2
>>> 3 - a
-2

####Augmented assignment

Here the reverse magic methods is the ‘i’ + ‘magic name’, just as __iadd__(self, other). The meaning is self+=other.

####Unary operator

Magic Methods Function
__neg__(self) ‘-x’
__pos__(self) ‘+x’
__abs__(self) ‘abs(x)’
__invert__(self) ‘~x : Bit (0 <-> 1)’

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!