How to do this in Python?

Terry Reedy tjreedy at udel.edu
Tue Mar 17 21:33:27 EDT 2009


Jim Garrison wrote:

> Ah.  That's the Pythonesque way I was looking for.  I knew
> it would be a generator/iterator but haven't got the Python
> mindset down yet and haven't played with writing my own
> generator.  I'm still trying to think in purely object-
> oriented terms where I would override __next__() to
> return a chunk of the appropriate size.
> 
> Give a man some code and you solve his immediate problem.
> Show him a pattern and you've empowered him to solve
> his own problems.  Thanks!

Python's iterator-fed for-loops are its primary motor for calculation. 
Anytime one thinks of processing successive items with a while-loop, one 
could consider factoring out the production of the successive items with 
an iterator.  While loops are really only needed for repeated processing 
of a single object.

tjr




More information about the Python-list mailing list