Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #25309
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
|---|---|
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: lambda in list comprehension acting funny |
| Date | Sat, 14 Jul 2012 03:31:12 -0700 (PDT) |
| Organization | http://groups.google.com |
| Lines | 43 |
| Message-ID | <34872b84-3724-41d0-92ca-b7fec60b0efc@googlegroups.com> (permalink) |
| References | <CADjSo4QvTnGKy_ZYOO5qZu3tSKovwAMGjku4L+RXEkYmwXPz4g@mail.gmail.com> <mailman.2007.1341988993.4697.python-list@python.org> <P%wLr.743666$N53.119162@fx20.am4> |
| NNTP-Posting-Host | 123.192.83.145 |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| X-Trace | posting.google.com 1342261873 14309 127.0.0.1 (14 Jul 2012 10:31:13 GMT) |
| X-Complaints-To | groups-abuse@google.com |
| NNTP-Posting-Date | Sat, 14 Jul 2012 10:31:13 +0000 (UTC) |
| In-Reply-To | <P%wLr.743666$N53.119162@fx20.am4> |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=123.192.83.145; posting-account=5JdMBQoAAABHnS4mjpqEzxnmWtgiiVNw |
| User-Agent | G2/1.0 |
| Xref | csiph.com comp.lang.python:25309 |
Show key headers only | View raw
Alister於 2012年7月12日星期四UTC+8下午5時44分15秒寫道: > On Wed, 11 Jul 2012 08:43:11 +0200, Daniel Fetchinson 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? > > > > And more importantly, what's the simplest way to achieve the latter? :) > > Having read Steve's explanation in the other thread (which I think has > finally flipped the light switch on lambda for me) it only requires a > minor change > > funcs=[ lambda x,y=i:x**y for i in range(5) ] > > although I cant actually think why this construct would be needed in > practice, how are you actually using it > > > -- > * Simunye is so happy she has her mothers gene's > <Dellaran> you better give them back before she misses them! Uhn, there are 5 objects in the list if not factored well to be executed in the run time.
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll 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