i=2; lst=[i**=2 while i<1000]

Duncan Booth duncan.booth at invalid.invalid
Tue Dec 6 11:08:05 EST 2005


Steve Holden wrote:

>>>>lst=[i**=2 while i<1000]
>>>>
>>>>of course this could be easily rewritten into
>>>>i=2
>>>>lst=[]
>>>>while i<1000:
>>>>    i**=2
>>>>    lst.append(i)
>>>>
>>>
...
> Don't you have an interpreter you could run the code in to verify that
> it does indeed loop interminably? You seem to be assuming that the 
> expression i**2 changes the value of i. It doesn't.

I think that like me you read 'i**2' when the OP actually wrote 'i**=2'. 
i**=2 will change the value of i in the conventional while loop. In the 
list comprehension it is of course a syntax error even if you assume list 
comprehensions are expanded to allow the while.



More information about the Python-list mailing list