-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathc4_02_delegating_iteration.py
74 lines (63 loc) · 1.94 KB
/
c4_02_delegating_iteration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#%%
"""
delegating iteration from container to inner items
yield in __iter__
"""
# %%
# in the iterator protocol, __iter__ usually returns self. However, you can also point to an attribute
class MyListWithMetaData:
def __init__(self, mylist, metadata):
self.mylist = mylist
self.metadata = metadata
def __iter__(self):
return self.mylist.__iter__()
aa = MyListWithMetaData([1,2,3], "extrainfo")
# %%
for el in aa:
print(el)
# %%
# * Because the __iter__ forwards the iteration to the inner list, no need to define a __next__. Indeed it would never be called here as the next that is called is the one of the list. Proof below
class MyListContainer1:
def __init__(self, mylist):
self.mylist = mylist
self.current_index = 0
def __iter__(self):
print("__iter__ is called")
return self
def __next__(self):
print("__next__ is called")
current_index = self.current_index
try:
val = self.mylist[current_index]
self.current_index +=1
return val
except IndexError:
raise StopIteration
# %%
for el in MyListContainer1([1,2,3]):
print(el) # ? What will be printed ?
# %%
class MyListContainer2:
def __init__(self, mylist):
self.mylist = mylist
def __iter__(self):
print("__iter__ is called")
return self.mylist.__iter__()
def __next__(self):
print("__next__ is called")
raise ValueError("Should not be accessed")
# %%
for el in MyListContainer2([1,2,3]):
print(el) # ? What will be printed ?
# %%
# ! Note : you can implement __iter__ as a generator if you dont want to use next
class MyListContainer3:
def __init__(self, mylist):
self.mylist = mylist
def __iter__(self):
print("__iter__ is called")
for el in self.mylist:
yield el
for el in MyListContainer3([1,2,3]):
print(el) # ? What will be printed ?
# %%