魔术方法

Python魔术方法

​ 在Python中,所有以双下划线__包起来的方法,统称为Magic Method(魔术方法),它是一种的特殊方法,普通方法需要调用,而魔术方法不需要调用就可以自动执行。

​ 魔术方法在类或对象的某些事件出发后会自动执行,让类具有神奇的“魔力”。如果希望根据自己的程序定制自己特殊功能的类,那么就需要对这些方法进行重写。

​ 魔术方法有上百种,只有很小的一部分我们才会使用,大部分魔术方法我们只需要了解即可

常见魔术方法

1.初始化方法__init__

1
2
3
4
触发机制:实例化对象之后立即触发
参数:至少有一个self,接收当前对象,其他参数根据需要进行定义
返回值:无
作用:初始化对象的成员

用得太多了

2.构造方法__new__

1
2
3
4
5
触发时机: 实例化对象时自动触发(在__init__之前触发)
参数:至少一个cls 接收当前类,其他参数根据初始化方法参数决定
返回值:必须返回一个对象实例,没有返回值,则实例化对象的结果为None
作用:实例化对象
注意:实例化对象是Object类底层实现,其他类继承了Object的__new__才能够实现实例化对象。

__new__ 方法用得很少,它用于建立一个类,几乎只有客制化类的时候才需要使用

1
2
3
4
5
6
7
8
9
10
class A:
def __new__(cls, x):
print('__new__')
return super().__new__(cls)

def __init__(self, x):
self.x = x
print('__init__')

o = A(1)

3.析构方法__del__

1
2
3
4
5
触发时机:当该类对象被销毁时,自动触发
参数:一个self,接受当前对象
返回值:无
作用:关闭或释放对象创建时资源
注意:del不一定会触发当前方法,只有当前对象没有任何变量引用时才会触发
1
2
3
4
5
6
7
8
class A:
def __del__(self):
print("__del__")

o = A(1)
x = o
del o
print("done")

4.__call__

callable是指能够当成函数调用的对象

1
2
3
4
5
调用对象的魔术方法
触发时机:将对象当作函数调用时触发,方式: 对象()
参数:至少一个self接收对象,其余根据调用时参数决定
返回值:根据情况而定
作用:可以将复杂的步骤进行合并操作,减少调用的步骤,方便使用
1
2
3
4
5
6
7
8
9
class Multiplier(object):
def __init__(self, mul):
self.mul = mul

def __call__(self, arg):
return self.mul * arg

o = Multiplier(3)
print(o(4))

5.__len__

1
2
3
4
5
触发时机:使用len(对象) 的时候触发
参数:一个参数self
返回值:必须是一个整型
作用:可以设置为检测对象成员个数,但是也可以进行其他任意操作
注意:返回值必须必须是整数,否则语法报错,另外该要求是格式要求。
1
2
3
4
5
6
7
8
9
class MyList(object):
def __init__(self, data):
self._data = data

def __len__(self):
return len(self._data)

x = MyList([1, 2])
print(len(x))

6.__getitem__

1
2
3
触发时机:使用对象[i]的时候触发
参数:一个self和索引
同理可以知道__setitem__,__delitem__,__reversed__(由reverse()函数触发)等方法的用法
1
2
3
4
5
6
7
8
9
class MyList(object):
def __init__(self, data):
self._data = data

def __getitem__(self, key):
return self._data[key]

x = MyList([1, 2])
print(x[0])

7.__repr__

1
2
3
4
触发时机:在使用repr(对象)的时候触发
参数:一个self接收对象
返回值:必须是字符串
作用:将对象转使用repr化为字符串时使用,也可以用于快捷操作

8.__format__

1
2
3
4
5
触发时机:使用字符串.format(对象)时候触发
参数:一个self接收对象,一个参数接收format的{}中的格式,例如:>5
返回值:必须是字符串
作用:设置对象可以作为format的参数,并且自定义对象格式化的规则
注意:无

魔术方法重载运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from math import hypot,sqrt
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y

#__str__方法,会在打印变量时,自动调用
def __str__(self):
return 'Vector(%r,%r)' %(self.x, self.y)

#__abs__函数会在调用abs()函数时,自动调用
def __abs__(self):
#hypot(x,y) 返回欧几里得范数 sqrt(x*x + y*y)
return hypot(self.x, self.y)

#__bool__函数会在调用bool()函数时,自动调用
def __bool__(self):
return bool(abs(self))

# 执行a + b时,自动调用
def __add__(self, other):
x = self.x + other.x
y = self.y + other.y
return Vector(x,y)

# 执行a+=b时,自动调用
def __iadd__(self, other):
return self + other

# 执a * b的时候,会自动调用
def __mul__(self, scalar):
return Vector(self.x * scalar,self.y * scalar)

# 求-a时,自动调用
def __neg__(self):
return Vector(-self.x,-self.y)

def __matmul__():
pass
v1 = Vector(2,4)
v2 = Vector(2,1)
print(v1 + v2)
v = Vector(3,4)
print(abs(v))
print(v * 3)
print(abs(v * 3))
print(-v)
v1 += v2
print(v1)

其它魔术方法:

比较运算相关魔术方法

1
2
3
4
5
6
7
8
9
10
11
12
__ lt__(self, other):
定义小于号的行为:x < y 调用 x.lt(y)
__ le__(self, other):
定义小于等于号的行为:x <= y 调用 x.le(y)
__ eq__(self, other) :
定义等于号的行为:x == y 调用 x.eq(y)
__ ne__(self, other):
定义不等号的行为:x != y 调用 x.ne(y)
__ gt__(self, other):
定义大于号的行为:x > y 调用 x.**gt(y)
__ ge__(self, other) :
定义大于等于号的行为:x >= y 调用 x.ge(y)

赋值运算相关魔术方法

1
2
3
4
5
6
7
8
9
10
11
12
__iadd__(self, other)             定义赋值加法的行为:+=
__isub__(self, other) 定义赋值减法的行为:-=
__imul__(self, other) 定义赋值乘法的行为:=
__itruediv__(self, other) 定义赋值真除法的行为:/=
__ifloordiv__(self, other) 定义赋值整数除法的行为://=
__imod__(self, other) 定义赋值取模算法的行为:%=
__ipow__(self, other[, modulo]) 定义赋值幂运算的行为:**=
__ilshift__(self, other) 定义赋值按位左移位的行为:<<=
__irshift__(self, other) 定义赋值按位右移位的行为:>>=
__iand__(self, other) 定义赋值按位与操作的行为:&=
__ixor__(self, other) 定义赋值按位异或操作的行为:^=
__ior__(self, other) 定义赋值按位或操作的行为:|=

类型转换相关魔术方法

1
2
3
4
5
6
7
__complex__(self)      定义当被 complex() 调用时的行为(需要返回恰当的值)
__int__(self) 定义当被 int() 调用时的行为(需要返回恰当的值)
__float__(self) 定义当被 float() 调用时的行为(需要返回恰当的值)
__round__(self[, n]) 定义当被 round() 调用时的行为(需要返回恰当的值)
__index(self)__ 1. 当对象是被应用在切片表达式中时,实现整形强制转换
2. 如果你定义了一个可能在切片时用到的定制的数值型,你应该定义 index
3. 如果 index 被定义,则 int 也需要被定义,且返回相同的值

上下文管理相关魔术方法(with)

1
2
3
4
5
6
7
8
__enter__` 和 `__exit__
__enter__(self)
1. 定义当使用 with 语句时的初始化行为
2. enter 的返回值被 with 语句的目标或者 as 后的名字绑定

__exit__(self, exctype, excvalue, traceback)
1. 定义当一个代码块被执行或者终止后上下文管理器应该做什么
2. 一般被用来处理异常,清除工作或者做一些代码块执行完毕之后的日常工作