empty lists vs empty generators

Andrea Griffini agriff at tin.it
Tue May 3 14:18:03 EDT 2005


On 2 May 2005 21:49:33 -0700, "Michele Simionato"
<michele.simionato at gmail.com> wrote:

>Starting from Python 2.4 we have tee in the itertools
>module, so you can define the following:
>
>from itertools import tee
>
>def is_empty(it):
>    it_copy = tee(it)[1]
>    try:
>        it_copy.next()
>    except StopIteration:
>        return True
>    else:
>        return False
>
>It works with generic iterables too.

Are you sure this is going to do the right thing ?
seems to me it would drop the first element of
"it"... (the yielded element entered the tee twins,
but already got out of "it").
I would say that unless you use the second twin
after calling is_empty that code wouldn't work...

Am I correct or instead "tee" uses black magic to
just peek at the yielded value without starting a
continuation ?

Andrea



More information about the Python-list mailing list