lazy evaluation is sometimes too lazy... help please.
Ken Pu
kenpuca.dev at gmail.com
Fri Jan 16 02:51:43 EST 2009
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
More information about the Python-list
mailing list