Generator question

Timothy Wu 2huggie at gmail.com
Sun Nov 26 04:55:10 EST 2006


On 11/26/06, Robert Kern <robert.kern at gmail.com> wrote:
>
>
> The only thing that the last line does is *create* a new generator object.
> You
> need to actually iterate over it and yield its values. E.g.
>
>
> In [2]: def test_gen(x):
>    ...:     yield x
>    ...:     x -= 1
>    ...:     if x != 0:
>    ...:         for y in test_gen(x):
>    ...:             yield y
>    ...:
>    ...:
>
> In [3]: list(test_gen(3))
> Out[3]: [3, 2, 1]



Ha-HA, that makes perfect sense I guess. Though in my opinion the definition
makes the code a bit harder to read. Thanks for the explanation.

Timothy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20061126/20e25756/attachment.html>


More information about the Python-list mailing list