my_iterator.py 1005 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from abc import ABCMeta, abstractmethod
  2. class my_iterator(object):
  3. """
  4. abstract class used to implement a collection class
  5. """
  6. __metaclass__ = ABCMeta
  7. def __init__(self):
  8. self.current = -1
  9. self.__count = 0
  10. def item(self, i):
  11. """
  12. item has to be inplemented in the main class
  13. """
  14. pass
  15. def count_update(self, count):
  16. self.__count = count
  17. return count
  18. def count(self):
  19. """
  20. returns the number of items
  21. """
  22. return self.__count
  23. def __iter__(self):
  24. """
  25. Python method to define and call the iterator
  26. """
  27. return self
  28. # used to define the iterator: next element
  29. def __next__(self):
  30. """
  31. Python method to calls the iterator next element
  32. """
  33. self.current += 1
  34. if self.current >= self.__count:
  35. self.current = -1
  36. raise StopIteration
  37. return self.item(self.current)