
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
(written 2008-11-04)
3898
Associated topics are indexed under
R104 - Ruby - Control Structures [3769] Muttable v immutable and implications - Ruby - (2012-06-20)
[3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
[3619] Ruby v Perl - a comparison example - (2012-02-21)
[3422] Assigning values to variables within other statements - Ruby - (2011-09-07)
[3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
[3254] Multiple inputs, multiple out, ruby functions - (2011-04-19)
[3253] Is this number between? Does this list include? - Ruby - (2011-04-18)
[3200] How a for loop works Java, Perl and other languages - (2011-03-12)
[3159] Returning multiple values from a function call in various languages - a comparison - (2011-02-06)
[3158] Ruby training - some fresh examples for string handling applications - (2011-02-05)
[3156] Splitting data reading code from data processing code - Ruby - (2011-02-04)
[2975] Why do I need brackets in Ruby ... or Perl, Python, C or Java - (2010-09-29)
[2892] Alternative loops and conditionals in Ruby and Perl - (2010-07-28)
[2711] For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07)
[2619] Passing code to procedures and yield in Ruby - (2010-02-02)
[2471] A short form of if ... then ... else - (2009-10-23)
[2287] Learning to program in Ruby - examples of the programming basics - (2009-07-15)
[1904] Ruby, Perl, Linux, MySQL - some training notes - (2008-11-23)
[1891] Ruby to access web services - (2008-11-16)
[1887] Ruby Programming Course - Saturday and Sunday - (2008-11-16)
[1738] Clean code, jump free (Example in Lua) - (2008-08-06)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[1587] Some Ruby programming examples from our course - (2008-03-21)
[1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[1163] A better alternative to cutting and pasting code - (2007-04-26)
[995] Ruby's case - no break - (2006-12-17)
[985] Equality in Ruby - == eql? and equal? - (2006-12-14)
[962] Breaking a loop - Ruby and other languages - (2006-12-03)
[960] 1st, 2nd, 3rd revisited in Ruby - (2006-12-02)
U114 - Lua - Threading and Coroutines. [3395] Parallel but not really parallel. Moving game characters. Coroutines in Lua. - (2011-08-17)
[2455] Lua examples - coroutines, error handling, objects, etc - (2009-10-15)
[2314] Passing parameters to a coroutine in Lua - (2009-08-01)
[1699] If you are learning Lua, here are some more examples - (2008-07-06)
[1691] Co-routines in Lua - co-operative processing - (2008-06-29)
Y105 - Python - Functions, Modules and Packages [4029] Exception, Lambda, Generator, Slice, Dict - examples in one Python program - (2013-03-04)
[3945] vargs in Python - how to call a method with unknown number of parameters - (2012-12-06)
[3931] Optional positional and named parameters in Python - (2012-11-23)
[3885] Default local - a good choice by the author of Python - (2012-10-08)
[3852] Static variables in Python? - (2012-08-29)
[3766] Python timing - when to use a list, and when to use a generator - (2012-06-16)
[3695] Functions are first class variables in Lua and Python - (2012-04-13)
[3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
[3474] Python Packages - groupings of modules. An introduction - (2011-10-11)
[3472] Static variables in functions - and better ways using objects - (2011-10-10)
[3464] Passing optional and named parameters to python methods - (2011-10-04)
[3459] Catching the fishes first? - (2011-09-27)
[3280] Passing parameters to Python functions - the options you have - (2011-05-07)
[2998] Using an exception to initialise a static variable in a Python function / method - (2010-10-13)
[2994] Python - some common questions answered in code examples - (2010-10-10)
[2929] Passing a variable number of parameters in to a function / method - (2010-08-20)
[2878] Program for reliability and efficiency - do not duplicate, but rather share and re-use - (2010-07-19)
[2766] Optional and named parameters to Python functions/methods - (2010-05-15)
[2718] Python - access to variables in the outer scope - (2010-04-12)
[2520] Global and Enable - two misused words! - (2009-11-30)
[2506] Good example of recursion in Python - analyse an RSS feed - (2009-11-18)
[2481] Sample code with errors in it on our web site - (2009-10-29)
[2440] Optional parameters to Python functions - (2009-10-07)
[2439] Multiple returns from a function in Python - (2009-10-06)
[2011] Conversion of OSI grid references to Eastings and Northings - (2009-01-28)
[1879] Dynamic code - Python - (2008-11-11)
[1871] Optional and named parameters in Python - (2008-11-05)
[1869] Anonymous functions (lambdas) and map in Python - (2008-11-04)
[1790] Sharing variables with functions, but keeping them local too - Python - (2008-09-09)
[1784] Global - Tcl, PHP, Python - (2008-09-03)
[1464] Python Script - easy examples of lots of basics - (2007-12-08)
[1202] Returning multiple values from a function (Perl, PHP, Python) - (2007-05-24)
[1134] Function / method parameters with * and ** in Python - (2007-04-04)
[959] It's the 1st, not the 1nd 1rd or 1th. - (2006-12-01)
[949] Sludge off the mountain, and Python and PHP - (2006-11-27)
[913] Python - A list of methods - (2006-11-03)
[912] Recursion in Python - (2006-11-02)
[900] Python - function v method - (2006-10-20)
[821] Dynamic functions and names - Python - (2006-08-03)
[775] Do not duplicate your code - (2006-06-23)
[749] Cottage industry or production line data handling methods - (2006-06-07)
[745] Python modules. The distribution, The Cheese Shop and the Vaults of Parnassus. - (2006-06-05)
[668] Python - block insets help with documentation - (2006-04-04)
[561] Python's Generator functions - (2006-01-11)
[418] Difference between import and from in Python - (2005-08-18)
[386] What is a callback? - (2005-07-22)
[340] Code and code maintainance efficiency - (2005-06-08)
[308] Call by name v call by value - (2005-05-11)
[303] Lambdas in Python - (2005-05-06)
[294] Python generator functions, lambdas, and iterators - (2005-04-28)
[105] Distance Learning - (2004-10-31)
[96] Variable Scope - (2004-10-22)
53df
Some other Articles
Is it worth it?List Comprehensions in PythonBarack Obama wins US PredidencyWhat to do with a huge crop of applesLiverpool - a friendly cityDomain Renewal GroupWhat a difference a day madeDebugging and Data::Dumper in Perl