Skip to content

Instantly share code, notes, and snippets.

@2tony2
Created July 5, 2024 16:11
Show Gist options
  • Save 2tony2/8271a994b4c0363435e59a29109f2187 to your computer and use it in GitHub Desktop.
Save 2tony2/8271a994b4c0363435e59a29109f2187 to your computer and use it in GitHub Desktop.
class MyIterator:
def __init__(self, data):
self.data = data
self.index = 0
def __iter__(self):
return self
def __next__(self):
if self.index < len(self.data):
item = self.data[self.index]
self.index += 1
return item
else:
raise StopIteration
my_iter = MyIterator([1, 2, 3])
for item in my_iter:
print(item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment