tentativa The functions generally follow the conventional process flow, return values and quit. Generators work similarly, but remember the state of the processing between calls, staying in memory and returning the next item expected when activated.
The generators have several advantages over conventional functions:
Generators are usually called through a for loop. The syntax is similar to the traditional function, just the yield instruction substitutes return. In each new iteraction, yield returns the next value.
def gen_pares(): """ Generates even numbers from 0 to 20 """ i = 0 while i <= 20: yield i i += 2 # Shows each number and goes to the next for n in gen_pares(): print n
0 2 4 6 8 10 12 14 16 18 20
import os # Finds files recursively def find(path='.'): for item in os.listdir(path): fn = os.path.normpath(os.path.join(path, item)) if os.path.isdir(fn): for f in find(fn): yield f else: yield fn # At each interaction, the generator yeld a new file name for fn in find(): print fn
There are many generators that are builtin to the lanaguage, like xrange(). Moreover, in the module itertools, many useful generators are defined.
To convert the output of a generator into a list:
alist = list(generator())
That way, all the items will be generated at once.