-
-
Save stephengruppetta/eb84e0883deb67323b976ae9d526a686 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 | |
numbers = MyList([2, 5, 10, 3, 99, 42]) | |
# Use numbers in 'map' | |
map(lambda x: x * 2, numbers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment