You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+29-18Lines changed: 29 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -1172,8 +1172,8 @@ Iterable Duck Types
1172
1172
-------------------
1173
1173
1174
1174
### Iterable
1175
-
***Only required method for iterable is iter(), that should return an iterator of its contents.**
1176
-
***Contains() is automatically generated whenever iter() is present.**
1175
+
***Only required method for iterable is iter(). It should return an iterator of its contents.**
1176
+
***Contains() automatically works on any object that has iter() defined.**
1177
1177
```python
1178
1178
classMyIterable:
1179
1179
def__init__(self, a):
@@ -1192,58 +1192,69 @@ True
1192
1192
```
1193
1193
1194
1194
### Collection
1195
-
***Every collection is also an iterable.**
1195
+
***Only required methods are iter() and len().**
1196
1196
***This cheatsheet actually means `'<iterable>'` when it uses `'<collection>'`.**
1197
1197
***I chose not to use the name iterable because it sounds scarier and more vague than collection.**
1198
1198
```python
1199
1199
classMyCollection:
1200
1200
def__init__(self, a):
1201
1201
self.a = a
1202
+
def__iter__(self):
1203
+
for el inself.a:
1204
+
yield el
1202
1205
def__contains__(self, el):
1203
1206
return el inself.a
1204
1207
def__len__(self):
1205
1208
returnlen(self.a)
1206
-
def__iter__(self):
1207
-
for el inself.a:
1208
-
yield el
1209
1209
```
1210
1210
1211
1211
### Sequence
1212
-
***Every sequence is also a collection.**
1213
-
***Iter()and reversed() are automatically generated whenever getitem() is present.**
1212
+
***Only required methods are len() and getitem().**
1213
+
***Iter(), contains() and reversed() automatically work on any object that has getitem() defined.**
1214
1214
```python
1215
1215
classMySequence:
1216
1216
def__init__(self, a):
1217
1217
self.a = a
1218
-
def__getitem__(self, i):
1219
-
returnself.a[i]
1218
+
def__iter__(self):
1219
+
for el inself.a:
1220
+
yield el
1221
+
def__contains__(self, el):
1222
+
return el inself.a
1220
1223
def__len__(self):
1221
1224
returnlen(self.a)
1225
+
def__getitem__(self, i):
1226
+
returnself.a[i]
1227
+
def__reversed__(self):
1228
+
returnreversed(self.a)
1222
1229
```
1223
1230
1224
1231
### Collections.abc.Sequence
1225
1232
***It's a richer interface than the basic sequence.**
1226
-
***Extending it generates contains(), iter(), reversed(), index(), and count().**
1233
+
***Extending it generates iter(), contains(), reversed(), index(), and count().**
1227
1234
***Unlike `'abc.Iterable'` and `'abc.Collection'`, it is not a duck type. That's why `'issubclass(MySequence, collections.abc.Sequence)'` would return 'False' even if it had all the methods defined.**
1228
1235
```python
1229
1236
classMyAbcSequence(collections.abc.Sequence):
1230
1237
def__init__(self, a):
1231
1238
self.a = a
1232
-
def__getitem__(self, i):
1233
-
returnself.a[i]
1234
1239
def__len__(self):
1235
1240
returnlen(self.a)
1241
+
def__getitem__(self, i):
1242
+
returnself.a[i]
1236
1243
```
1237
1244
1238
-
#### Table of the methods that each (duck) type provides:
<li><strong>Every collection is also an iterable.</strong></li>
1108
+
<li><strong>Only required methods are iter() and len().</strong></li>
1109
1109
<li><strong>This cheatsheet actually means <codeclass="python hljs"><spanclass="hljs-string">'<iterable>'</span></code> when it uses <codeclass="python hljs"><spanclass="hljs-string">'<collection>'</span></code>.</strong></li>
1110
1110
<li><strong>I chose not to use the name iterable because it sounds scarier and more vague than collection.</strong></li>
<li><strong>It's a richer interface than the basic sequence.</strong></li>
1139
-
<li><strong>Extending it generates contains(), iter(), reversed(), index(), and count().</strong></li>
1146
+
<li><strong>Extending it generates iter(), contains(), reversed(), index(), and count().</strong></li>
1140
1147
<li><strong>Unlike <codeclass="python hljs"><spanclass="hljs-string">'abc.Iterable'</span></code> and <codeclass="python hljs"><spanclass="hljs-string">'abc.Collection'</span></code>, it is not a duck type. That's why <codeclass="python hljs"><spanclass="hljs-string">'issubclass(MySequence, collections.abc.Sequence)'</span></code> would return 'False' even if it had all the methods defined.</strong></li>
0 commit comments