回答編集履歴
1
誤字修正
answer
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
|
1
|
+
以下にリストのイテレータを書きました。このコードの中で **状態** というのは `self._index` になります。状態というよりもリストの **現在位置** みたいなものですかね。
|
2
2
|
|
3
3
|
```python
|
4
4
|
class Iterator:
|
5
5
|
def __init__(self, container):
|
6
6
|
self._container = container
|
7
|
-
self._index = 0
|
7
|
+
self._index = 0 # <--- これです
|
8
8
|
|
9
9
|
def __iter__(self):
|
10
10
|
return self
|
@@ -18,9 +18,15 @@
|
|
18
18
|
raise StopIteration
|
19
19
|
|
20
20
|
|
21
|
-
|
21
|
+
iterator = Iterator(['Yaruo', 'Yaranaio', 'Yarumi'])
|
22
22
|
for element in iterator:
|
23
23
|
print(element)
|
24
24
|
```
|
25
|
-
|
25
|
+
```
|
26
|
+
...
|
27
|
+
Yaruo
|
28
|
+
Yaranaio
|
29
|
+
Yarumi
|
30
|
+
>>>
|
31
|
+
```
|
26
32
|
* [Python のイテレータってなに?](https://python.ms/type/for/iterator/)
|