Yield
Richard Brodie
R.Brodie at rl.ac.uk
Thu Nov 16 10:49:16 EST 2006
"Danny Colligan" <dannycolligan at gmail.com> wrote in message
news:1163691173.737911.130410 at i42g2000cwa.googlegroups.com...
> Now that we're on the subject, what are the advantages of using
> generators over, say, list comprehensions or for loops? It seems to me
> that virtually all (I won't say everything) the examples I've seen can
> be done just as easily without using generators.
The more trivial the example, the harder it is to see the advantage.
Suppose you wanted to sum the first 10000 primes. A quick Google
fins you Wensheng Wang's recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/366178
Just add print sum(primes(10000)), and you're done.
More information about the Python-list
mailing list