Differences between revisions 2 and 3
Revision 2 as of 2004-11-17 05:53:23
Size: 1126
Editor: netcache7
Comment:
Revision 3 as of 2005-04-02 19:12:36
Size: 2536
Editor: aaron
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
= simple examples = = Examples =
Line 37: Line 38:
= Generators made from classes? = to be written: Generators made from classes?

== Links ==

 * [http://www.python.org/peps/pep-0255.html PEP-255: Simple Iterators] -- the original
 * [http://www-106.ibm.com/developerworks/library/l-pycon.html?n-l-9271 Iterators and Simple Generators]
 * [http://www-106.ibm.com/developerworks/linux/library/l-cpyiter.html combinatorial functions in itertools]
 * [http://linuxgazette.net/100/pramode.html Python Generator Tricks] -- various infinite sequences, recursions, ...
 * [http://www-106.ibm.com/developerworks/linux/library/l-pythrd.html "weightless threads"] -- simulating threads using generators
 * [http://www-106.ibm.com/developerworks/library/x-tipgenr.html XML processing] -- yes, using generators
 * [http://c2.com/cgi/wiki?GeneratorsAreNotCoroutines C2:GeneratorsAreNotCoroutines] -- particulars on generators, coroutines, and continuations

= Discussion =

I once saw MikeOrr demonstrate Before and After examples. But, I forget how they worked.

Can someone demonstrate here?

He did something like: Show how a normal list operation could be written to use generators. Something like:

{{{
#!python
def double(L):
    return [x*2 for x in L]

eggs = double([1, 2, 3, 4, 5])
}}}

...he showed how that, or something like that, could be rewritten using iterators, generators.

It's been a while since I've seen it, I may be getting this all wrong.

-- LionKimbro [[DateTime(2005-04-02T19:12:19Z)]]

Generators are very useful and can help simplify your code and improve its performance.

Examples

For example, the RangeGenerator can be used to iterate over a large number of values, without creating a massive list (like range would)

   1 #the for loop will generate each i (i.e. 1,2,3,4,5, ...), add it to sum,  and throw it away
   2 #before the next i is generated.  This is opposed to iterating through range(...), which creates
   3 #a potentially massive list and then iterates through it.
   4 for i in irange(1000000):
   5    sum = sum+i

Generators can be composed. Here we create a generator on the squares of consecutive integers.

   1 #square is a generator 
   2 square = (i*i for i in irange(1000000))
   3 #add the squares
   4 for i in square:
   5    sum = sum +i

Here, we compose a square generator with the takewhile generator, to generate squares less than 100

   1 #add squares less than 100
   2 square = (i*i for i in count())
   3 bounded_squares = takewhile(lambda x : x< 100, square)
   4 for i in bounded_squares:
   5    sum += i

to be written: Generators made from classes?

Discussion

I once saw MikeOrr demonstrate Before and After examples. But, I forget how they worked.

Can someone demonstrate here?

He did something like: Show how a normal list operation could be written to use generators. Something like:

   1 def double(L):
   2     return [x*2 for x in L]
   3 
   4 eggs = double([1, 2, 3, 4, 5])

...he showed how that, or something like that, could be rewritten using iterators, generators.

It's been a while since I've seen it, I may be getting this all wrong.

-- LionKimbro DateTime(2005-04-02T19:12:19Z)

Generators (last edited 2020-12-04 19:05:45 by ChrisM)

Unable to edit the page? See the FrontPage for instructions.