Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: lazy evaluation is sometimes too lazy... help please. (http://www.velocityreviews.com/forums/t665825-re-lazy-evaluation-is-sometimes-too-lazy-help-please.html)

Boris Borcic 01-16-2009 10:37 AM

Re: lazy evaluation is sometimes too lazy... help please.
 
The minimal correction, I guess, is to write

itlist[i] = (x+(i*10) for i in [i] for x in count())

instead of

itlist[i] = (x+(i*10) for x in count())

although

itlist[i] = (x+(i*10) for i,s in (i,count()) for x in s)

will better mimic generalizations in the sense that the "minimal correction"
delays the evaluation of count(), which doesn't matter in the case of count(),
but might make a difference if you replace it with some other expression.

The point is that the first iterator-producing expression in a generator
expression - eg the <X> in (<exp> for vars in <X>...) - is evaluated immediately
in the surrounding context, in contrast to the rest of the genexp.

Cheers, BB

Ken Pu wrote:
> Hi, below is the code I thought should create two generates, it[0] =
> 0,1,2,3,4,5, and it[1] = 0,10,20,30,..., but they turn out to be the
> same!!!
>
> from itertools import *
> itlist = [0,0]
> for i in range(2):
> itlist[i] = (x+(i*10) for x in count())
>
> print "what's in the bags:"
> print list(islice(itlist[0], 5))
> print list(islice(itlist[1], 5))
>
> The output is:
> [10, 11, 12, 13, 14]
> [10, 11, 12, 13, 14]
>
> I see what Python is doing -- lazy evaluation doesn't evaluate
> (x+(i*10) for x in count()) until the end. But is this the right
> behaviour? How can I get the output I want:
> [0, 1, 2, 3, 4]
> [10, 11, 12, 13, 14]
>
> Thanks.
>
> Ken
> --
> http://mail.python.org/mailman/listinfo/python-list
>




All times are GMT. The time now is 10:46 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.