Skip to content

Commit befee8a

Browse files
committed
Duck types
1 parent d82fab7 commit befee8a

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,17 +1154,13 @@ class MyOpen():
11541154
Hello World!
11551155
```
11561156

1157-
#### Context managers:
1157+
#### List of existing context managers:
11581158
```python
11591159
with open('<path>', ...) as file: ...
11601160
with wave.open('<path>', ...) as wave_file: ...
11611161
with memoryview(<bytes/bytearray/array>) as view: ...
1162-
```
1163-
1164-
#### Reusable context managers:
1165-
```python
1162+
db = sqlite3.connect('<path>'); with db: db.execute('<insert_query>')
11661163
lock = threading.RLock(); with lock: ...
1167-
con = sqlite3.connect('<path>'); with con: con.execute('<insert_query>')
11681164
```
11691165

11701166

@@ -1232,6 +1228,7 @@ class MySequence:
12321228
* **It's a richer interface than the basic sequence.**
12331229
* **Extending it generates iter(), contains(), reversed(), index(), and count().**
12341230
* **Unlike `'abc.Iterable'` and `'abc.Collection'`, it is not a duck type. That is why `'issubclass(MySequence, collections.abc.Sequence)'` would return 'False' even if it had all the methods defined.**
1231+
12351232
```python
12361233
class MyAbcSequence(collections.abc.Sequence):
12371234
def __init__(self, a):
@@ -1258,6 +1255,8 @@ class MyAbcSequence(collections.abc.Sequence):
12581255
+------------+----------+------------+----------+--------------+
12591256
```
12601257

1258+
* **Other useful ABCs that automatically generate missing methods are: MutableSequence, Set, MutableSet, Mapping and MutableMapping.**
1259+
12611260

12621261
Enum
12631262
----

index.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,14 +1075,12 @@ <h3 id="contextmanager">Context Manager</h3>
10751075
<span class="hljs-meta">... </span> print(file.read())
10761076
Hello World!
10771077
</code></pre>
1078-
<h4 id="contextmanagers">Context managers:</h4>
1078+
<h4 id="listofexistingcontextmanagers">List of existing context managers:</h4>
10791079
<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: ...
10801080
<span class="hljs-keyword">with</span> wave.open(<span class="hljs-string">'&lt;path&gt;'</span>, ...) <span class="hljs-keyword">as</span> wave_file: ...
10811081
<span class="hljs-keyword">with</span> memoryview(&lt;bytes/bytearray/array&gt;) <span class="hljs-keyword">as</span> view: ...
1082-
</code></pre>
1083-
<h4 id="reusablecontextmanagers">Reusable context managers:</h4>
1084-
<pre><code class="python language-python hljs">lock = threading.RLock(); <span class="hljs-keyword">with</span> lock: ...
1085-
con = sqlite3.connect(<span class="hljs-string">'&lt;path&gt;'</span>); <span class="hljs-keyword">with</span> con: con.execute(<span class="hljs-string">'&lt;insert_query&gt;'</span>)
1082+
db = sqlite3.connect(<span class="hljs-string">'&lt;path&gt;'</span>); <span class="hljs-keyword">with</span> db: db.execute(<span class="hljs-string">'&lt;insert_query&gt;'</span>)
1083+
lock = threading.RLock(); <span class="hljs-keyword">with</span> lock: ...
10861084
</code></pre>
10871085
<h2 id="iterableducktypes"><a href="#iterableducktypes" name="iterableducktypes">#</a>Iterable Duck Types</h2>
10881086
<h3 id="iterable">Iterable</h3>
@@ -1168,6 +1166,9 @@ <h4 id="tableofrequiredandavailablespecialmethods">Table of required and availab
11681166
┃ count() │ │ │ │ ✓ ┃
11691167
┗━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━━━━━┛
11701168
</code></pre>
1169+
<ul>
1170+
<li><strong>Other useful ABCs that automatically generate missing methods are: MutableSequence, Set, MutableSet, Mapping and MutableMapping.</strong></li>
1171+
</ul>
11711172
<h2 id="enum"><a href="#enum" name="enum">#</a>Enum</h2>
11721173
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> enum <span class="hljs-keyword">import</span> Enum, auto
11731174

0 commit comments

Comments
 (0)