-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcounter.html
264 lines (255 loc) · 18.1 KB
/
counter.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Counters in python — bits v0.8 documentation</title>
<link rel="stylesheet" href="_static/sphinxdoc.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '',
VERSION: '0.8',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="bits v0.8 documentation" href="index.html" />
<link rel="next" title="Surprising imports" href="package.html" />
<link rel="prev" title="Manipulating bitfields in Python (in most language actually)" href="bitfield.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="package.html" title="Surprising imports"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="bitfield.html" title="Manipulating bitfields in Python (in most language actually)"
accesskey="P">previous</a> |</li>
<li><a href="index.html">bits v0.8 documentation</a> »</li>
</ul>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Counters in python</a><ul>
<li><a class="reference internal" href="#the-object-oriented-way">the object oriented way</a></li>
<li><a class="reference internal" href="#with-a-function-and-a-function-attribute">with a function and a function attribute</a></li>
<li><a class="reference internal" href="#the-trick-of-the-argument-default-value">the trick of the argument default value</a></li>
<li><a class="reference internal" href="#using-yield">using <em>yield</em></a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="bitfield.html"
title="previous chapter">Manipulating bitfields in Python (in most language actually)</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="package.html"
title="next chapter">Surprising imports</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/counter.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" size="18" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="counters-in-python">
<h1>Counters in python<a class="headerlink" href="#counters-in-python" title="Permalink to this headline">¶</a></h1>
<p>Counters serve many purposes in software development. It is sometimes
handy to use a incrementor function which <em>returns</em> the new value
every time it is called, like calling <tt class="docutils literal"><span class="pre">count()</span></tt> instead of
manipulating directly a variable by using a binding statement such
as``count += 1``. It has its uses especially in Python where binding a
variable does not return the bound value, as in the C langage.</p>
<p><em>Update: itertools.count() is in the standard library</em></p>
<p>There is a peculiar <a class="reference external" href="http://docs.python.org/tutorial/controlflow.html#lambda-forms">example</a> in the official Python documentation on
<em>lambda</em>. The <em>make_incrementor</em> function is misleadingly named since
nothing is incremented: the created callable merely returns the
addition of two integers.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">make_incrementor</span><span class="p">(</span><span class="n">start</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">return</span> <span class="k">lambda</span> <span class="n">jump</span><span class="p">:</span> <span class="n">jump</span> <span class="o">+</span> <span class="n">start</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">f</span> <span class="o">=</span> <span class="n">make_incrementor</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">f</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="go">42</span>
<span class="gp">>>> </span><span class="n">f</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">43</span>
</pre></div>
</div>
<p>An incrementor would commonly be called without argument and, in this
example, return <em>43</em> the first time it was called and <em>44</em> the second
time, etc. With an incrementor, same cause lead to <em>different</em>
effects.</p>
<p>In the example, <em>start</em> is enclosed in the created callable <em>f</em> but it
can not be modified. The following function tries to increment
<em>start</em>. But an <em>unbound exception</em> concerning <em>start</em> is raised when
calling the incrementor:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">make_incrementor</span><span class="p">(</span><span class="n">start</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">jump</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">start</span><span class="o">+=</span><span class="n">jump</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">start</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">f</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">f</span> <span class="o">=</span> <span class="n">make_incrementor</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">f</span><span class="p">()</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="nc">UnboundLocalError</span>: <span class="n-Identifier">local variable 'start' referenced before assignment</span>
</pre></div>
</div>
<p>This variable can’t be written in this scope, have a look at four correct
implementations below.</p>
<div class="section" id="the-object-oriented-way">
<h2>the object oriented way<a class="headerlink" href="#the-object-oriented-way" title="Permalink to this headline">¶</a></h2>
<p>The counter enclosed in the incrementor instance is an attribute
initialised by the constructor at the instanciation step. When the
instance is called, the attribute is incremented by one, and the value
returned:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">class</span> <span class="nc">make_incrementor</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">start</span><span class="p">):</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span><span class="o">=</span><span class="n">start</span>
<span class="gp">...</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">jump</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">+=</span> <span class="n">jump</span>
<span class="gp">... </span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">f</span> <span class="o">=</span> <span class="n">make_incrementor</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">f</span><span class="p">(),</span> <span class="n">f</span><span class="p">(),</span> <span class="n">f</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="go">(43, 44, 54)</span>
</pre></div>
</div>
<p>The object oriented version is a bit heavy but is the most explicit:
the three steps of class definition, object instanciation and object
manipulation are clearly separated.</p>
</div>
<div class="section" id="with-a-function-and-a-function-attribute">
<h2>with a function and a function attribute<a class="headerlink" href="#with-a-function-and-a-function-attribute" title="Permalink to this headline">¶</a></h2>
<p>A function can be used, it is a function which returns another
function, but in the end, it is the same as a class instantiating an
object. In Python, functions are also object and as such, can have
attributes too:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">make_incrementor</span><span class="p">(</span><span class="n">start</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">jump</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">f</span><span class="o">.</span><span class="n">count</span><span class="o">+=</span><span class="n">jump</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">f</span><span class="o">.</span><span class="n">count</span>
<span class="gp">... </span> <span class="n">f</span><span class="o">.</span><span class="n">count</span><span class="o">=</span><span class="n">start</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">f</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">f</span> <span class="o">=</span> <span class="n">make_incrementor</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">f</span><span class="p">(),</span> <span class="n">f</span><span class="p">(),</span> <span class="n">f</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="go">(43, 44, 54)</span>
</pre></div>
</div>
<p>It is a bit less clear here, what is executed and when. The rule is
simple, the body of function is parsed but not evaluated until the
function is called, only the function is defined, and its default
value evaluated...</p>
</div>
<div class="section" id="the-trick-of-the-argument-default-value">
<h2>the trick of the argument default value<a class="headerlink" href="#the-trick-of-the-argument-default-value" title="Permalink to this headline">¶</a></h2>
<p>Here we make the closest attempt to build a closure, the counter is
initialised with the default value when the function is defined, and
there is no reference to the counter outside the function.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">make_incrementor</span><span class="p">(</span><span class="n">start</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">jump</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="p">[</span><span class="n">start</span><span class="p">]):</span>
<span class="gp">... </span> <span class="n">count</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+=</span> <span class="n">jump</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">count</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">f</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">f</span> <span class="o">=</span> <span class="n">make_incrementor</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">f</span><span class="p">(),</span> <span class="n">f</span><span class="p">(),</span> <span class="n">f</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="go">(43, 44, 54)</span>
</pre></div>
</div>
<p><em>count</em> is a reference to a list whose first element is the real
counter integer. In Python, you can’t manipulate reference to integer,
so the trick is to pass them enclosed in a list which are passed by
reference. If <em>count</em> is a pointer on an integer, in C, the pointer
and the value would be written <tt class="docutils literal"><span class="pre">count</span></tt> and <tt class="docutils literal"><span class="pre">*count</span></tt>, in Python,
you get a similar result by defining <em>count</em> as list of one integer
element, and writing <tt class="docutils literal"><span class="pre">count</span></tt> and <tt class="docutils literal"><span class="pre">count[0]</span></tt>.</p>
<p>The list or dictionnary default arguments in a function signature are
only evaluated once at the declaration time. Whenever the function is
called later on, the count list is not reset to <em>[start]</em>, whic make it
possible to store data between calls, as if count was a <em>static</em> variable.</p>
</div>
<div class="section" id="using-yield">
<h2>using <em>yield</em><a class="headerlink" href="#using-yield" title="Permalink to this headline">¶</a></h2>
<p>The following function uses <em>yield</em>. Yielding, for a function, is the
act of voluntarily suspending itself. Functions using yield return a
generator which have the <em>next()</em> and <em>send()</em> methods.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">make_incrementor</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">jump</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">count</span> <span class="o">=</span> <span class="n">start</span>
<span class="gp">... </span> <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">count</span> <span class="o">+=</span> <span class="n">jump</span>
<span class="gp">... </span> <span class="n">jump</span> <span class="o">=</span> <span class="p">(</span><span class="k">yield</span> <span class="n">count</span><span class="p">)</span> <span class="ow">or</span> <span class="mi">1</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">f</span> <span class="o">=</span> <span class="n">make_incrementor</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">f</span><span class="o">.</span><span class="n">next</span><span class="p">(),</span> <span class="n">f</span><span class="o">.</span><span class="n">next</span><span class="p">(),</span> <span class="n">f</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="go">(43, 44, 54)</span>
</pre></div>
</div>
<p>As generators are functions which can be resumed, they keep their
state: they can keep track of a counter.</p>
<p>Which counter implementation do you prefer?</p>
<p><em>10 May 2009</em></p>
</div>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="package.html" title="Surprising imports"
>next</a> |</li>
<li class="right" >
<a href="bitfield.html" title="Manipulating bitfields in Python (in most language actually)"
>previous</a> |</li>
<li><a href="index.html">bits v0.8 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2009, Jean Daniel Browne.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.4.
</div>
</body>
</html>