Skip to content

Instantly share code, notes, and snippets.

@JackInTaiwan
Last active October 19, 2019 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JackInTaiwan/64f08a6b5a3bb0107d6cc203b3e2154b to your computer and use it in GitHub Desktop.
Save JackInTaiwan/64f08a6b5a3bb0107d6cc203b3e2154b to your computer and use it in GitHub Desktop.
python_iteration_4.py
class MyIterator:
def __init__(self, max_num):
self.max_num = max_num
def __getitem__(self, key):
if key <= self.max_num:
return key
else:
raise IndexError
my_iterator = MyIterator(3)
for item in my_iterator:
print(item)
# 0
# 1
# 2
# 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment