generator function

C

chansky

I read the following link about generator:
http://www.python.org/peps/pep-0255.html
but I am still not so clear on the use/purpose of a generator function
other than the fact that a generator can retain the state of the local
variables within the fuction body. Can someone out there shed some
lights on this topic or share about how/when you would ever use a
generator function.
 
S

Skip Montanaro

tom> I read the following link about generator:
tom> http://www.python.org/peps/pep-0255.html
tom> but I am still not so clear on the use/purpose of a generator function
tom> other than the fact that a generator can retain the state of the local
tom> variables within the fuction body.

That's generally a more than sufficient reason. Have a look at the
SpamBayes tokenizer module and the several tokenize* methods and functions
it contains for some fairly elaborate examples:

http://tinyurl.com/jaun

Skip
 
T

Terry Reedy

chansky said:
I read the following link about generator:
http://www.python.org/peps/pep-0255.html
but I am still not so clear on the use/purpose of a generator function
other than the fact that a generator can retain the state of the local
variables within the fuction body. Can someone out there shed some
lights on this topic or share about how/when you would ever use a
generator function.

Besides the other answers, Lib/test/test_generators.py, at 40K,
contains a tutorial and lots of examples, include some that many would
not readily think of.

TJR
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top