-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaula161.py
More file actions
42 lines (33 loc) · 1.1 KB
/
Copy pathaula161.py
File metadata and controls
42 lines (33 loc) · 1.1 KB
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
# Implementando o protocolo do Iterator em Python
# Essa é apenas uma aula para introduzir os protocolos de collections.abc no Python.
# Qualquer outro protocolo poderá ser implementado seguindo a mesma estrutura usada nessa aula.
# https://docs.python.org/3/library/collections.abc.html
from collections.abc import Sequence
class MyList(Sequence):
def __init__(self):
self._data = {}
self._index = 0
self._next_index = 0
def append(self, value):
self._data[self._index] = value
self._index += 1
def __len__(self) -> int:
return self._index
def __getitem__(self, index):
return self._data[index]
def __iter__(self):
return self
def __next__(self):
if self._next_index >= self._index:
raise StopIteration
value = self._data[self._next_index]
self._next_index += 1
return value
if __name__ == "__main__":
lista = MyList()
lista.append('Fernando')
lista.append('João')
# print(lista[0])
# print(len(lista))
for item in lista:
print(item)