Skip to content

Instantly share code, notes, and snippets.

@MattWoodhead
Created August 2, 2017 21:15
Show Gist options
  • Save MattWoodhead/38619991284b81ae073993f3895badd2 to your computer and use it in GitHub Desktop.
Save MattWoodhead/38619991284b81ae073993f3895badd2 to your computer and use it in GitHub Desktop.
""" A test class showing how to add iterators to classes """
class TodoList(object):
""" A test class showing how to add iterators to classes """
def __init__(self):
self.tasks = [] # initialise empty list
def __iter__(self):
return iter(self.tasks)
def add_task(self, task):
self.tasks.append(task)
LIST = TodoList()
for i in range(10): # create a task list
task = f"task {str(i+1).zfill(2)}"
LIST.add_task(task)
for task in LIST: # iterate over the instance object
print(task)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment