Skip to content

Instantly share code, notes, and snippets.

@niconico25
Last active January 16, 2019 07:11
Show Gist options
  • Save niconico25/3dfc60e757806121355b129cc0b4135e to your computer and use it in GitHub Desktop.
Save niconico25/3dfc60e757806121355b129cc0b4135e to your computer and use it in GitHub Desktop.
# 対話モード >>> に
# コピペで実行できます。
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