On Jul 11, 11:41 am, Daniel Fetchinson <fetchin...@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> -- http://mail.python.org/mailman/listinfo/python-list