lambda in list comprehension acting funny

Daniel Fetchinson fetchinson at googlemail.com
Wed Jul 11 08:43:11 CEST 2012


> funcs = [ lambda x: x**i for i in range( 5 ) ]
> print funcs[0]( 2 )
> print funcs[1]( 2 )
> print funcs[2]( 2 )
>
> This gives me
>
> 16
> 16
> 16
>
> When I was excepting
>
> 1
> 2
> 4
>
> Does anyone know why?

And more importantly, what's the simplest way to achieve the latter? :)


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown



More information about the Python-list mailing list