fig3.py 455 B

123456789101112131415
  1. class Iterable(object):
  2. def __init__(self):
  3. self.arr = range(10)
  4. def __iter__(self):
  5. # This iterable returns *itself* as the iterator, and keeps track of
  6. # where it is with a private variable (indicated by the underscore)
  7. self._current_index = 0
  8. return self
  9. def __next__(self):
  10. # Get item and update which index we're returning next
  11. item = self.arr[self._current_index]
  12. self._current_index += 1
  13. return item