-
Notifications
You must be signed in to change notification settings - Fork 0
/
python-protocol.html
213 lines (174 loc) · 12.9 KB
/
python-protocol.html
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<!DOCTYPE html>
<!--[if IE 8]> <html class="no-js lt-ie9" lang="zh-cn"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh-cn"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>道可叨 | Python 中的接口 </title>
<meta name="viewport" content="width=device-width">
<meta name="author" content="Qiang">
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
<link href="http://localhost:8000/feeds/atom.xml" type="application/atom+xml" rel="alternate" title="道可叨 Atom Feed" />
<link rel="stylesheet" href="https://zhuoqiang.github.io/theme/css/app.css">
<script src="https://zhuoqiang.github.io/theme/js/vendor/custom.modernizr.js"></script>
</head>
<body>
<header>
<hgroup>
<a href="https://zhuoqiang.github.io">
<h1 i18n>道可叨</h1>
<h2>Free Will</h2>
</a>
</hgroup>
</header>
<section>
<article>
<header>
<h1><a href="https://zhuoqiang.github.io/python-protocol.html">Python 中的接口</a></h1>
</header> <div>
<section>
<p>Python 是动态类型语言, 只在运行时做 Duck Typing 检查.</p>
<ul>
<li>利: 灵活, 方便</li>
<li>弊: 代码混乱, 缺少规范</li>
</ul>
<p>标准自带两类接口支持: <code>abc</code> 和 <code>typing.Protocol</code>, 有他们协助给天马行空的程序员套上枷锁, Python 的大工程才可以"上道"</p>
<h2><a href="https://docs.python.org/3/library/abc.html"><code>abc</code></a></h2>
<p>abc 就是 Abstract Base Class, 虚基类. 跟 Java, C++ 中的虚基类是一个意思, 可以对派生类提供实例化时的动态检查, 确保虚拟接口 (<code>abstractmethod</code>) 都有实现</p>
<div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">abc</span>
<span class="k">class</span> <span class="nc">Base</span><span class="p">(</span><span class="n">abc</span><span class="o">.</span><span class="n">ABC</span><span class="p">):</span>
<span class="nd">@abstractmethod</span>
<span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">s</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
<span class="sd">"""abc interface demo</span>
<span class="sd"> """</span>
<span class="k">class</span> <span class="nc">Invalid</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">class</span> <span class="nc">Child</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">pass</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">Child</span><span class="p">()</span>
<span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">Base</span><span class="p">)</span>
<span class="c1"># TypeError: Can't instantiate abstract class Invalid with abstract methods foo</span>
<span class="n">i</span> <span class="o">=</span> <span class="n">Invalid</span><span class="p">()</span>
</code></pre></div>
<p>也提供了非侵入式的虚基类关联方法</p>
<div class="highlight"><pre><span></span><code><span class="kn">from</span> <span class="nn">abc</span> <span class="kn">import</span> <span class="n">ABC</span>
<span class="k">class</span> <span class="nc">MyABC</span><span class="p">(</span><span class="n">ABC</span><span class="p">):</span>
<span class="k">pass</span>
<span class="n">MyABC</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="nb">tuple</span><span class="p">)</span>
<span class="k">assert</span> <span class="nb">issubclass</span><span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="n">MyABC</span><span class="p">)</span>
<span class="k">assert</span> <span class="nb">isinstance</span><span class="p">((),</span> <span class="n">MyABC</span><span class="p">)</span>
</code></pre></div>
<ul>
<li>检查时机: 在运行中当派生类实例化时</li>
<li>检查范围: 只确保 <code>abstractmethod</code> 是否在派生类中有相同函数名实现, 并不检查实现的参数和返回值是否相同. 只看名字不比签名</li>
<li>代码影响: 侵入式, 需要继承. 也有手工非侵入式方案</li>
</ul>
<h2><a href="https://docs.python.org/3/library/typing.html#typing.Protocol"><code>typing.Protocol</code></a></h2>
<p>structure subtyping (static duck-typing)</p>
<div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">typing</span>
<span class="k">class</span> <span class="nc">Countable</span><span class="p">(</span><span class="n">typing</span><span class="o">.</span><span class="n">Protocol</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">count</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">who</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
<span class="sd">"""support count</span>
<span class="sd"> """</span>
<span class="k">class</span> <span class="nc">Counter</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">count</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">who</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
<span class="k">return</span> <span class="mi">0</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">c</span><span class="p">:</span> <span class="n">Countable</span><span class="p">):</span>
<span class="n">c</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="s2">"bill"</span><span class="p">)</span>
</code></pre></div>
<ul>
<li>检查时机: 静态类型检查接口使用方, 例如 mypy</li>
<li>检查范围: 确保实现类按照签名实现了接口的全部函数</li>
<li>代码影响: 非侵入式, 不需要继承</li>
</ul>
<h2>比较</h2>
<p><code>abc</code> 类似 c++ 中的虚基类, <code>typing.Protocol</code> 则好比 c++ 中的 concept.</p>
<p>当然, Python 是动态语言, 在 <a href="https://docs.python.org/3/library/typing.html#typing.runtime_checkable"><code>typing.runtime_checkable</code></a> 和 <code>abc.abstractmethod</code> 加成后, <code>typing.Protocol</code> 动静两相宜</p>
<div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">typing</span>
<span class="nd">@typing</span><span class="o">.</span><span class="n">runtime_checkable</span>
<span class="k">class</span> <span class="nc">Countable</span><span class="p">(</span><span class="n">typing</span><span class="o">.</span><span class="n">Protocol</span><span class="p">):</span>
<span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
<span class="k">def</span> <span class="nf">count</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">who</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
<span class="sd">"""support count</span>
<span class="sd"> """</span>
<span class="k">class</span> <span class="nc">Counter</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">count</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">who</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span><span class="p">:</span>
<span class="k">return</span> <span class="mi">0</span>
<span class="k">assert</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">Counter</span><span class="p">,</span> <span class="n">Countable</span><span class="p">)</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">()</span>
<span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">Countable</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">c</span><span class="p">:</span> <span class="n">Countable</span><span class="p">):</span>
<span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">Countable</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="s2">"bill"</span><span class="p">))</span>
<span class="n">f</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">InvalidCounter</span><span class="p">(</span><span class="n">Countable</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">c</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">pass</span>
<span class="c1"># TypeError: Can't instantiate abstract class InvalidCounter with abstract methods count</span>
<span class="n">i</span> <span class="o">=</span> <span class="n">InvalidCounter</span><span class="p">()</span>
</code></pre></div>
<p>上面这个终极解决方案兼有两者的优点:</p>
<ul>
<li>静态类型检查时会确保是否在派生类中有相同签名的实现</li>
<li>动态运行时, 会检查是否同名函数存在</li>
<li>代码影响: 自动非侵入式, 不需要继承, 也无需手工注册</li>
</ul>
</section>
</div>
<footer>
<p>
<time datetime="2020-07-20T00:00:00+08:00" pubdate>2020-07-20</time><a href="https://zhuoqiang.github.io/category/programming.html"><span class="category" i18n>programming</span></a>
</p>
<ul>
<li><a href="https://zhuoqiang.github.io/tag/python.html"><span class="tag" i18n>python</span></i></a>
<li><a href="https://zhuoqiang.github.io/tag/design.html"><span class="tag" i18n>design</span></i></a>
</ul>
</footer><link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
<script src="https://unpkg.com/gitalk@latest/dist/gitalk.min.js"></script>
<div id="gitalk-container"></div>
<script type="text/javascript">
var gitalk = new Gitalk({
clientID: "2f60008869581fd61d19",
clientSecret: "6b303dcf58c04cd35782bae82540921663cbfda8",
repo: "zhuoqiang.github.com",
owner: "zhuoqiang",
admin: ["zhuoqiang"],
id: "python-protocol",
title: "Python 中的接口",
perPage: 30,
distractionFreeMode: true,
pagerDirection: "last",
});
gitalk.render('gitalk-container');
</script></article>
</section>
<footer>
<p>@ <a href="mailto:zhuo.qiang@gmail.com"><span i18n>Qiang</span></a> | <span class="heart"><a href="https://github.com/getpelican/pelican">Pelican</a> & <a href="https://bitbucket.org/zhuoqiang/jing">Jing</a></span></p>
</footer>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"></script>
<script>window.jQuery || document.write('<script src="https://zhuoqiang.github.io/theme/js/vendor/jquery-1.8.3.min.js"><\/script>')</script>
<script src="https://zhuoqiang.github.io/theme/js/vendor/moment.min.js"></script>
<script src="https://zhuoqiang.github.io/theme/js/vendor/moment-lang.min.js"></script>
<script src="https://zhuoqiang.github.io/theme/js/vendor/i18next-1.6.0.min.js"></script>
<script src="https://zhuoqiang.github.io/theme/js/main.js"></script>
<script type="text/javascript">
var GoSquared = {};
GoSquared.acct = "GSN-743443-H";
(function(w){
function gs(){
w._gstc_lt = +new Date;
var d = document, g = d.createElement("script");
g.type = "text/javascript";
g.src = "//d1l6p2sc9645hc.cloudfront.net/tracker.js";
var s = d.getElementsByTagName("script")[0];
s.parentNode.insertBefore(g, s);
}
w.addEventListener ?
w.addEventListener("load", gs, false) :
w.attachEvent("onload", gs);
})(window);
</script>
</body>
</html>