Files
example-code-2e/iterables/sentence_gen.py
2014-10-14 14:26:55 -03:00

24 lines
408 B
Python

"""
Sentence: iterate over words using a generator function
"""
import re
import reprlib
RE_WORD = re.compile('\w+')
class Sentence:
def __init__(self, text):
self.text = text
def __repr__(self):
return 'Sentence(%s)' % reprlib.repr(self.text)
def __iter__(self):
for match in RE_WORD.finditer(self.text): # <1>
yield match.group() # <2>
# done! <3>