2536
Comment:
|
2562
|
Deletions are marked like this. | Additions are marked like this. |
Line 50: | Line 50: |
See also: ["Iterator"] |
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)
Generators can be composed. Here we create a generator on the squares of consecutive integers.
Here, we compose a square generator with the takewhile generator, to generate squares less than 100
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
See also: ["Iterator"]
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:
...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.