[Python-ideas] [Python-Dev] yield * (Re: Missing operator.call)

Antoine Pitrou solipsis at pitrou.net
Mon Feb 9 14:10:54 CET 2009


Steven D'Aprano <steve at ...> writes:
> 
> Marcin 'Qrczak' Kowalczyk wrote:
> > On Sun, Feb 8, 2009 at 23:20, Steven D'Aprano <steve at ...> wrote:
> > 
> >> And the cost is small:
> >>
> >> [steve <at> ando ~]$ python -m timeit -s "seq = range(500)" "(3*x for x in
seq if
> >> x%2 == 1)"
> >> 1000000 loops, best of 3: 0.611 usec per loop
> > 
> > Because generators are lazy and you don't run it into completion.
> 
> We were talking about the cost of *making* the generator, not the cost 
> of running it to completion.

No, I was talking about the cost of running it to completion. A generator is
executed in a separated frame. Therefore, if you "yield from" a generator, there
is a frame switch at each iteration between the generator frame and the frame of
the "yield from".
Which is not the case with an inline "for" loop containing a "yield".

Regards

Antoine.





More information about the Python-ideas mailing list