Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import abc
- import reprlib
- class Sentence:
- def __init__(self, text):
- self.text = text
- self.words = text.split(' ')
- def __getitem__(self, index):
- return self.words[index]
- def __repr__(self):
- return f'Sentence({reprlib.repr(self.text)})'
- if __name__ == '__main__':
- print(f'Sentence iterable?: {issubclass(Sentence, abc.Iterable)}')
- s = Sentence('Hello, Python world.')
- s_iter = iter(s)
- print(next(s_iter))
- print(next(s_iter))
- print(next(s_iter))
- print(next(s_iter))
- """
- Sentence iterable?: False
- Hello,
- Python
- world.
- Traceback (most recent call last):
- File "uniterable_sentence.py", line 24, in <module>
- print(next(s_iter))
- StopIteration
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement