Skip to content

Commit bf33efc

Browse files
committed
Iterable duck types
1 parent 19d53ae commit bf33efc

File tree

2 files changed

+4
-8
lines changed

2 files changed

+4
-8
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,8 +1207,7 @@ class MyCollection:
12071207
def __init__(self, a):
12081208
self.a = a
12091209
def __iter__(self):
1210-
for el in self.a:
1211-
yield el
1210+
return iter(self.a)
12121211
def __contains__(self, el):
12131212
return el in self.a
12141213
def __len__(self):
@@ -1224,8 +1223,7 @@ class MySequence:
12241223
def __init__(self, a):
12251224
self.a = a
12261225
def __iter__(self):
1227-
for el in self.a:
1228-
yield el
1226+
return iter(self.a)
12291227
def __contains__(self, el):
12301228
return el in self.a
12311229
def __len__(self):

index.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,8 +1122,7 @@ <h3 id="collection">Collection</h3>
11221122
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
11231123
self.a = a
11241124
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__iter__</span><span class="hljs-params">(self)</span>:</span>
1125-
<span class="hljs-keyword">for</span> el <span class="hljs-keyword">in</span> self.a:
1126-
<span class="hljs-keyword">yield</span> el
1125+
<span class="hljs-keyword">return</span> iter(self.a)
11271126
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__contains__</span><span class="hljs-params">(self, el)</span>:</span>
11281127
<span class="hljs-keyword">return</span> el <span class="hljs-keyword">in</span> self.a
11291128
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__len__</span><span class="hljs-params">(self)</span>:</span>
@@ -1139,8 +1138,7 @@ <h3 id="sequence">Sequence</h3>
11391138
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, a)</span>:</span>
11401139
self.a = a
11411140
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__iter__</span><span class="hljs-params">(self)</span>:</span>
1142-
<span class="hljs-keyword">for</span> el <span class="hljs-keyword">in</span> self.a:
1143-
<span class="hljs-keyword">yield</span> el
1141+
<span class="hljs-keyword">return</span> iter(self.a)
11441142
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__contains__</span><span class="hljs-params">(self, el)</span>:</span>
11451143
<span class="hljs-keyword">return</span> el <span class="hljs-keyword">in</span> self.a
11461144
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__len__</span><span class="hljs-params">(self)</span>:</span>

0 commit comments

Comments
 (0)