⇤ ← Revision 1 as of 2004-11-11 18:09:07
1059
Comment: simple examples of generators
|
1126
|
Deletions are marked like this. | Additions are marked like this. |
Line 6: | Line 6: |
#!python | |
Line 16: | Line 17: |
#!python | |
Line 22: | Line 24: |
Line 26: | Line 29: |
#!python | |
Line 33: | Line 37: |
= Generators made from classes? = |
Generators are very useful and can help simplify your code and improve its performance.
simple 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