[Tutor] Should I use generators here?

Kent Johnson kent37 at tds.net
Tue May 9 14:58:11 CEST 2006


Kent Johnson wrote:
> Generators are not an optimization technique so much as a way to 
> structure code that includes iteration.

This recipe is a good example of how using a simple generator can 
encapsulate a bit of processing that might be awkward to include in a 
larger processing loop. Generators are really useful when you want to 
process items in a sequence, producing a new sequence, and the 
processing requires maintaining some state. A generator lets you 
encapsulate the state and the logic using the state into a function that 
is usable by client code as a simple sequence.

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496682

Kent



More information about the Tutor mailing list