Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 対話モード >>> に
# コピペで実行できます。
class Container:
def __init__(self, list_):
self._list = list_
def __len__(self):
return len(self._list)
def __getitem__(self, index):
return self._list[index]
# container.__iter__()
def __iter__(self):
return Iterator(self)
class Iterator:
def __init__(self, container):
self._container = container
self._index = 0
# iterator.__iter__()
def __iter__(self):
return self
# iterator.__next__()
def __next__(self):
if self._index < len(self._container):
element = self._container[self._index]
self._index += 1
return element
else:
raise StopIteration
container = Container(['Yaruo', 'Yaranaio', 'Yarumi'])
for element in container:
print(element)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.