Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def print_empty_line():
- print('')
- ###
- class DataHolder(object):
- def __init__(self):
- self.counter = 0
- self.data = ['Hi', 'Ho', 'Foo', 'Bar']
- def __iter__(self):
- # This should return an iterator
- # In this case return self
- return self
- def __next__(self):
- if self.counter < len(self.data):
- self.counter += 1
- return self.data[self.counter - 1]
- else:
- # So we can use again
- self.counter = 0
- raise StopIteration()
- ###
- dataholder = DataHolder()
- for x in dataholder:
- print(x)
- print_empty_line()
- for x in dataholder:
- print(x)
- ###
- def my_generator(n):
- # print('Begin')
- counter = 1
- while counter <= n:
- # print('Before yield')
- yield counter * counter
- # print('After yield')
- counter += 1
- # print('End')
- print_empty_line()
- for x in my_generator(5):
- print(x)
Add Comment
Please, Sign In to add comment