Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. from collections import abc
  2. import reprlib
  3.  
  4.  
  5. class Sentence:
  6. def __init__(self, text):
  7. self.text = text
  8. self.words = text.split(' ')
  9.  
  10. def __getitem__(self, index):
  11. return self.words[index]
  12.  
  13. def __repr__(self):
  14. return f'Sentence({reprlib.repr(self.text)})'
  15.  
  16.  
  17. if __name__ == '__main__':
  18. print(f'Sentence iterable?: {issubclass(Sentence, abc.Iterable)}')
  19. s = Sentence('Hello, Python world.')
  20. s_iter = iter(s)
  21. print(next(s_iter))
  22. print(next(s_iter))
  23. print(next(s_iter))
  24. print(next(s_iter))
  25. """
  26. Sentence iterable?: False
  27. Hello,
  28. Python
  29. world.
  30. Traceback (most recent call last):
  31. File "uniterable_sentence.py", line 24, in <module>
  32. print(next(s_iter))
  33. StopIteration
  34. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement