|
1082 | 1082 | <span class="hljs-meta">>>> </span>next(counter), next(counter), next(counter)
|
1083 | 1083 | (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
|
1084 | 1084 | </code></pre>
|
1085 |
| -<div><h3 id="callable">Callable</h3><ul> |
| 1085 | +<div><h4 id="pythonhasmanydifferentiteratorobjects">Python has many different iterator objects:</h4><ul> |
| 1086 | +<li><strong>Iterators returned by the <a href="#iterator">iter()</a> function, such as list_iterator and set_iterator.</strong></li> |
| 1087 | +<li><strong>Objects returned by the <a href="#itertools">itertools</a> module, such as count, repeat and cycle.</strong></li> |
| 1088 | +<li><strong>Generators returned by the <a href="#generator">generator functions</a> and <a href="#comprehension">generator expressions</a>.</strong></li> |
| 1089 | +<li><strong>All <a href="#file">file objects</a>, etc.</strong></li> |
| 1090 | +</ul><div><h3 id="callable">Callable</h3><ul> |
1086 | 1091 | <li><strong>All functions and classes have a call() method, hence are callable.</strong></li>
|
1087 | 1092 | <li><strong>When this cheatsheet uses <code class="python hljs"><span class="hljs-string">'<function>'</span></code> for an argument, it actually means <code class="python hljs"><span class="hljs-string">'<callable>'</span></code>.</strong></li>
|
1088 | 1093 | </ul><pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Counter</span>:</span>
|
|
1091 | 1096 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__call__</span><span class="hljs-params">(self)</span>:</span>
|
1092 | 1097 | self.i += <span class="hljs-number">1</span>
|
1093 | 1098 | <span class="hljs-keyword">return</span> self.i
|
1094 |
| -</code></pre></div> |
| 1099 | +</code></pre></div></div> |
| 1100 | + |
| 1101 | + |
1095 | 1102 |
|
1096 | 1103 |
|
1097 | 1104 | <pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>counter = Counter()
|
|
1118 | 1125 | <span class="hljs-meta">... </span> print(file.read())
|
1119 | 1126 | Hello World!
|
1120 | 1127 | </code></pre>
|
1121 |
| -<div><h4 id="listofcoveredcontextmanagers">List of covered context managers:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> open(<span class="hljs-string">'<path>'</span>) <span class="hljs-keyword">as</span> file: ... |
1122 |
| -<span class="hljs-keyword">with</span> wave.open(<span class="hljs-string">'<path>'</span>) <span class="hljs-keyword">as</span> wave_file: ... |
1123 |
| -<span class="hljs-keyword">with</span> memoryview(<bytes/bytearray/array>) <span class="hljs-keyword">as</span> view: ... |
1124 |
| -<span class="hljs-keyword">with</span> concurrent.futures.ThreadPoolExecutor() <span class="hljs-keyword">as</span> executor: ... |
1125 |
| -db = sqlite3.connect(<span class="hljs-string">'<path>'</span>); <span class="hljs-keyword">with</span> db: ... |
1126 |
| -lock = threading.RLock(); <span class="hljs-keyword">with</span> lock: ... |
1127 |
| -</code></pre></div> |
1128 |
| - |
1129 | 1128 | <div><h2 id="iterableducktypes"><a href="#iterableducktypes" name="iterableducktypes">#</a>Iterable Duck Types</h2><div><h3 id="iterable">Iterable</h3><ul>
|
1130 | 1129 | <li><strong>Only required method is iter(). It should return an iterator of object's items.</strong></li>
|
1131 | 1130 | <li><strong>Contains() automatically works on any object that has iter() defined.</strong></li>
|
|
0 commit comments