python-抽象-概念与基础
python中的抽象被称为 duck typing
模式,如果它看起来像一只鸭子并且叫起来像一只鸭子,那么它大概就是一只鸭子。
有很多种方式实现抽象,特点也不同
- 使用
NotImplementedError
,这种也是最基础使用最多的方式
NotImplemented
return
一个没有实现的标记,表示可以由其他对象实现
NotImplementError
raise
一个没有实现的错误,必须实现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
50
51
52
53
54
55
56
57class A:
def __init__(self, v):
self.v = v
def __eq__(self, other):
print('A eq')
if isinstance(other, A):
return self.v == other.v
if isinstance(other, B):
return other.v == self.v
if isinstance(other,C):
return False
return NotImplemented
class B:
def __init__(self, v):
self.v = v
def __eq__(self, other):
print('B eq')
if isinstance(other, B):
return self.v == other.v
return NotImplemented
class C:
def __eq__(self, other):
raise NotImplementedError
a = A(1)
b = B(3)
c = C()
print(a == b)
print('-'*20)
print(b == a)
print('-'*20)
print(a == c)
print('-'*20)
print(b == c)
### 输出为
A eq
False
--------------------
B eq
A eq
False
--------------------
A eq
False
--------------------
B eq
Traceback (most recent call last):
File "/Users/didi/code/python/test/pack.py", line 45, in <module>
print(b == c)
File "/Users/didi/code/python/test/pack.py", line 34, in __eq__
raise NotImplementedError
NotImplementedError
可以看出来
a==b
调用的过程是A::__eq__
,因为A::__eq__
里实现了与B
的比较,- 而
b==a
调用过程是B::__eq__->A::__eq__
,因为B::__eq__
中没有实现与A
的比较,所以返回了一个NotImplemented
,这样会去调用A::__eq__
,所以得到了False
a == c b == c
与上面类似,不过因为C::__eq__
直接抛出了一个NotImplementedError
所以程序直接报错了
- 元类 见 python-抽象-metaclass
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 SHIELD!
评论