-
-
Save stephengruppetta/ba89e54376861b1bd771ae7db5962ccf 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 MyList: | |
def __init__(self, iterable): | |
self.items = iterable | |
def __iter__(self): | |
print(f"Calling __iter__ in MyList") | |
return MyListIterator(self.items) | |
class MyListIterator: | |
def __init__(self, iterable): | |
self.items = iterable | |
self.index = 0 | |
def __iter__(self): | |
print("Only called when iterating over iterator itself") | |
return self | |
def __next__(self): | |
if self.index < len(self.items): | |
item = self.items[self.index] | |
self.index += 1 | |
return item | |
else: | |
raise StopIteration |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment