Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #25167

Re: lambda in list comprehension acting funny

References <CADjSo4QvTnGKy_ZYOO5qZu3tSKovwAMGjku4L+RXEkYmwXPz4g@mail.gmail.com>
Date 2012-07-11 08:43 +0200
Subject Re: lambda in list comprehension acting funny
From Daniel Fetchinson <fetchinson@googlemail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2007.1341988993.4697.python-list@python.org> (permalink)

Show all headers | View raw


> 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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: lambda in list comprehension acting funny Daniel Fetchinson <fetchinson@googlemail.com> - 2012-07-11 08:43 +0200
  Re: lambda in list comprehension acting funny Franck Ditter <franck@ditter.org> - 2012-07-12 08:49 +0200
  Re: lambda in list comprehension acting funny Alister <alister.ware@ntlworld.com> - 2012-07-12 09:44 +0000
    Re: lambda in list comprehension acting funny Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-12 11:29 +0000
    Re: lambda in list comprehension acting funny 88888 Dihedral <dihedral88888@googlemail.com> - 2012-07-14 03:31 -0700

csiph-web