Odd behaviour with list comprehension

2008-02-29 Thread Ken Pu
Hi all, I observed an interesting yet unpleasant variable scope behaviour with list comprehension in the following code: print [x for x in range(10)] print x It outputs: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 9 So the list comprehension actually creates a variable x which is somewhat unexpected. Is th

lazy evaluation is sometimes too lazy... help please.

2009-01-15 Thread Ken Pu
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(itli