Last active
January 16, 2019 07:11
-
-
Save niconico25/3dfc60e757806121355b129cc0b4135e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 対話モード >>> に | |
# コピペで実行できます。 | |
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