fork download
  1. #!/usr/bin/env python3
  2.  
  3. class Obj():
  4. def __init__(self, word):
  5. self.word = word
  6.  
  7. def __iter__(self):
  8. return Iterator(self.word)
  9.  
  10. class Iterator:
  11. def __init__(self, word):
  12. self.word = word
  13. self.index = 0
  14.  
  15. def __next__(self):
  16. try:
  17. letter = self.word[self.index]
  18. self.index += 1
  19. return letter
  20. except IndexError:
  21. raise StopIteration()
  22.  
  23. def __iter__(self):
  24. return self
  25.  
  26.  
  27. obj = Obj('sergey')
  28. it = iter(obj)
  29.  
  30. print(next(it))
  31. print(next(it))
  32. print(next(it))
  33. print(next(it))
  34. print(next(it))
  35. print(next(it))
  36.  
Success #stdin #stdout 0s 27704KB
stdin
Standard input is empty
stdout
s
e
r
g
e
y