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 __iter__(self):
- print('Sentence __iter__ called.')
- for word in self.words:
- yield word
- 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(f's_iter: {s_iter}')
- for word in s_iter:
- print(word)
- """
- Sentence iterable?: True
- s_iter: <generator object Sentence.__iter__ at 0x7f402519ee58>
- Sentence __iter__ called.
- Hello,
- Python
- world.
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement