Archive - Originally posted on "The Horse's Mouth" - 2008-11-04 18:43:46 - Graham Ellis
Last year, you had a good crop of apples on your tree .... what did you do with them? Make apple pies for all the neighbours! How?
• You collect all the apples and bring them into the kitchen.
• You prepare the apples ready for the pies.
• You make the pastry and apple pies.
• You cook the pies
• You distribute them.
This year, you had a huge crop of apples, and you wanted to make apple pies ... not for all the neighbours, but for the whole town. How did you do that? The same way that you did last year? Alas, no. There were far too many appled to bring into the kitchen, all your bowls would have been filled with apples long before you had prepared them all, and your oven has only got limited capacity. Far better to collect a few, prepare a few, make a few pies, and distribute them ... and keep repeating that until you have fed the whole town.
Which is what a generator function does in Python. When you call a generator function (as the list provider in a for statement) in Python, it will yield when it has the first result available, which you can then process. Come back to the for statement, and it will continue to run the generator code ('pick the next batch of apples') and return the next result, time after time, until you're done.
Here's an example that's doing just this - opening a file in a generator function, and returning at each call the next line containing the word "Python". The loop in the main code is then extracting the person's name from the line, printing it out, and going back to the generator for the next "Python" line.
def tap(file):
fh = open (file,"r")
for line in fh.xreadlines():
if (line.find("Python") > 0):
yield line
# Following line just added to show that the tap
# method returns a generator
print tap("../requests.xyz")
for staff in tap("../requests.xyz"):
els = staff.split(" ")
print els[0]
Here is the sample output:
earth-wind-and-fire:~/nov08 grahamellis$ python dfg
<generator object at 0x414e0>
hazel
leane
olivia
adam
barry
harry
ken
nigel
rupert
earth-wind-and-fire:~/nov08 grahamellis$
See here for full source code of this example and here for details of our Python course