Something is rotten in Denmark...

harrismh777 harrismh777 at charter.net
Tue May 31 17:53:13 EDT 2011


Martin Manns wrote:
> After being confused I figured out it is a 3.x example:

Actually, it is a compatibility example between 2.x and 3.x, compare 
below for different behavior from two seemingly identical compatible 
constructs, one from 3.2, and the other from 2.6.4:


> Python 3.2 (r32:88445, Mar 29 2011, 21:33:57)
> [GCC 4.3.3] on linux2

>>>> fs=[]
>>>> fs = [(lambda n: i + n) for i in range(10)]
>>>> [fs[i](1) for i in range(10)]
> [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]       <=========== compare
>>>>


> Python 2.6.4 (r264:75706, Dec  7 2009, 18:45:15)
> [GCC 4.4.1] on linux2

>>>> fs=[]
>>>> fs = [(lambda n: i + n) for i in range(10)]
>>>> [fs[i](1) for i in range(10)]
> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]               <=========== compare
>>>>


      Having compared the two, someone please tell me whether the two 
are incompatible, mostly compatible, completely incompatible, or 
different languages...


   ,.,, I realize how 3.2 is working (at the moment) but as compared 
with the books, and the behavior of 2.6, it sure looks 'broke' to me...

... why would we want to defer lookup of the 'i' in range(10) until the 
anonymous function is called, instead of the time that the function 
object is returned... inquiring minds want to know...


PS  Ian calls the second construct "working by mistake..."







kind regards,
m harris







More information about the Python-list mailing list