Created
August 2, 2017 21:15
-
-
Save MattWoodhead/38619991284b81ae073993f3895badd2 to your computer and use it in GitHub Desktop.
""" A test class showing how to add iterators to classes """
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 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