Guest User

Untitled

a guest
Jun 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. def print_empty_line():
  2. print('')
  3.  
  4.  
  5. ###
  6.  
  7. class DataHolder(object):
  8.  
  9. def __init__(self):
  10. self.counter = 0
  11. self.data = ['Hi', 'Ho', 'Foo', 'Bar']
  12.  
  13. def __iter__(self):
  14. # This should return an iterator
  15. # In this case return self
  16. return self
  17.  
  18. def __next__(self):
  19. if self.counter < len(self.data):
  20. self.counter += 1
  21. return self.data[self.counter - 1]
  22. else:
  23. # So we can use again
  24. self.counter = 0
  25. raise StopIteration()
  26.  
  27.  
  28. ###
  29.  
  30. dataholder = DataHolder()
  31.  
  32. for x in dataholder:
  33. print(x)
  34.  
  35. print_empty_line()
  36. for x in dataholder:
  37. print(x)
  38.  
  39.  
  40. ###
  41.  
  42. def my_generator(n):
  43. # print('Begin')
  44. counter = 1
  45. while counter <= n:
  46. # print('Before yield')
  47. yield counter * counter
  48. # print('After yield')
  49. counter += 1
  50. # print('End')
  51.  
  52.  
  53. print_empty_line()
  54. for x in my_generator(5):
  55. print(x)
Add Comment
Please, Sign In to add comment