Skip to content

Instantly share code, notes, and snippets.

@luizomf
Last active July 14, 2022 14:25
Show Gist options
  • Save luizomf/9b3d9de89e0321885cf2823b92896e62 to your computer and use it in GitHub Desktop.
Save luizomf/9b3d9de89e0321885cf2823b92896e62 to your computer and use it in GitHub Desktop.
python-iterable-iterator.py
class MinhaLista:
def __init__(self):
self.__data = {}
self.__index = 0
self.__next_index = 0
def add(self, *values):
for value in values:
self.__data[self.__index] = value
self.__index += 1
def __check_index(self, index):
if not self.__data.get(index):
raise IndexError(f'Index {index} does not exist.')
def __setitem__(self, index, value):
self.__check_index(index)
self.__data[index] = value
def __getitem__(self, index):
self.__check_index(index)
return self.__data[index]
def __delitem__(self, index):
self.__check_index(index)
self.__data[index] = None
def __iter__(self):
return self
def __next__(self):
if self.__next_index >= self.__index:
self.__next_index = 0
raise StopIteration
value = self.__data.get(self.__next_index)
self.__next_index += 1
return value
def __str__(self) -> str:
return f'{self.__class__.__name__}({self.__data})'
if __name__ == '__main__':
lista = MinhaLista()
lista.add('Maria', 'João')
lista.add('Helena')
lista.add('Júlia')
lista[0] = 'Pedro'
del lista[0]
for item in lista:
print(item)
for item in lista:
print(item)
print(lista)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment