lambda in list comprehension acting funny
rusi
rustompmody at gmail.com
Fri Jul 13 00:33:40 EDT 2012
More information about the Python-list mailing list
Fri Jul 13 00:33:40 EDT 2012
- Previous message (by thread): lambda in list comprehension acting funny
- Next message (by thread): lambda in list comprehension acting funny
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Jul 11, 11:41 am, Daniel Fetchinson <fetchin... at googlemail.com> wrote: > 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? > > Cheers, > Daniel Your expectations are reasonable. Heres the equivalent in Haskell from which python has taken comprehensions. --------------------------------- GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help Prelude> let funcs = [ (\ x -> x ^ i)| i <- [0..4]] Prelude> (funcs !! 0)(2) 1 Prelude> (funcs !! 1)(2) 2 Prelude> (funcs !! 2)(2) 4 Prelude>
- Previous message (by thread): lambda in list comprehension acting funny
- Next message (by thread): lambda in list comprehension acting funny
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list