Skip to content

Instantly share code, notes, and snippets.

@stephengruppetta
Created June 25, 2023 09:14
Show Gist options
  • Save stephengruppetta/939f34383b99fddba521a78aaab12c2b to your computer and use it in GitHub Desktop.
Save stephengruppetta/939f34383b99fddba521a78aaab12c2b to your computer and use it in GitHub Desktop.
class AnotherIterable:
def __init__(self, iterable):
self.values = iterable
def __iter__(self):
return iter(self.values)
def __getitem__(self, item):
print(
f"AnotherIterable.__getitem__() has just "
f"been called with argument {item}"
)
return self.values[item]
my_iterable = AnotherIterable([2, 4, 6, 8])
print("Ready to start the 'for' loop…\n")
for item in my_iterable:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment