Skip to content

Commit 52fd3af

Browse files
committed
Iterator
1 parent a165025 commit 52fd3af

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

README.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,12 @@ class Counter:
11441144
(1, 2, 3)
11451145
```
11461146

1147+
#### Python has many different iterator objects:
1148+
* **Iterators returned by the [iter()](#iterator) function, such as list\_iterator and set\_iterator.**
1149+
* **Objects returned by the [itertools](#itertools) module, such as count, repeat and cycle.**
1150+
* **Generators returned by the [generator functions](#generator) and [generator expressions](#comprehension).**
1151+
* **All [file objects](#file), etc.**
1152+
11471153
### Callable
11481154
* **All functions and classes have a call() method, hence are callable.**
11491155
* **When this cheatsheet uses `'<function>'` for an argument, it actually means `'<callable>'`.**
@@ -1184,16 +1190,6 @@ class MyOpen():
11841190
Hello World!
11851191
```
11861192

1187-
#### List of covered context managers:
1188-
```python
1189-
with open('<path>') as file: ...
1190-
with wave.open('<path>') as wave_file: ...
1191-
with memoryview(<bytes/bytearray/array>) as view: ...
1192-
with concurrent.futures.ThreadPoolExecutor() as executor: ...
1193-
db = sqlite3.connect('<path>'); with db: ...
1194-
lock = threading.RLock(); with lock: ...
1195-
```
1196-
11971193

11981194
Iterable Duck Types
11991195
-------------------

index.html

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,12 @@
10821082
<span class="hljs-meta">&gt;&gt;&gt; </span>next(counter), next(counter), next(counter)
10831083
(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)
10841084
</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>
10861091
<li><strong>All functions and classes have a call() method, hence are callable.</strong></li>
10871092
<li><strong>When this cheatsheet uses <code class="python hljs"><span class="hljs-string">'&lt;function&gt;'</span></code> for an argument, it actually means <code class="python hljs"><span class="hljs-string">'&lt;callable&gt;'</span></code>.</strong></li>
10881093
</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,7 +1096,9 @@
10911096
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__call__</span><span class="hljs-params">(self)</span>:</span>
10921097
self.i += <span class="hljs-number">1</span>
10931098
<span class="hljs-keyword">return</span> self.i
1094-
</code></pre></div>
1099+
</code></pre></div></div>
1100+
1101+
10951102

10961103

10971104
<pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>counter = Counter()
@@ -1118,14 +1125,6 @@
11181125
<span class="hljs-meta">... </span> print(file.read())
11191126
Hello World!
11201127
</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">'&lt;path&gt;'</span>) <span class="hljs-keyword">as</span> file: ...
1122-
<span class="hljs-keyword">with</span> wave.open(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-keyword">as</span> wave_file: ...
1123-
<span class="hljs-keyword">with</span> memoryview(&lt;bytes/bytearray/array&gt;) <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">'&lt;path&gt;'</span>); <span class="hljs-keyword">with</span> db: ...
1126-
lock = threading.RLock(); <span class="hljs-keyword">with</span> lock: ...
1127-
</code></pre></div>
1128-
11291128
<div><h2 id="iterableducktypes"><a href="#iterableducktypes" name="iterableducktypes">#</a>Iterable Duck Types</h2><div><h3 id="iterable">Iterable</h3><ul>
11301129
<li><strong>Only required method is iter(). It should return an iterator of object's items.</strong></li>
11311130
<li><strong>Contains() automatically works on any object that has iter() defined.</strong></li>

0 commit comments

Comments
 (0)