What makes an iterator an iterator?

Steven D'Aprano steve at REMOVEME.cybersource.com.au
Thu Apr 19 23:34:09 EDT 2007


On Thu, 19 Apr 2007 20:00:31 -0700, Alex Martelli wrote:

> class sane(object):
>     def __init__(self, sentence='four scores and twenty years ago'):
>         def agenerator():
>             for word in sentence.split(): yield word
>         self._thegen = agenerator()
>     def __iter__(self): return self
>     def next(self): return self._thegen.next()


Nice technique! I always forget about nesting functions like that. I
should use it more often.



-- 
Steven D'Aprano 





More information about the Python-list mailing list