Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #25189
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <fetchinson@googlemail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'defaults': 0.05; 'needed,': 0.05; '*not*': 0.07; 'function,': 0.07; 'objects,': 0.07; 'parameter': 0.07; 'subject:skip:c 10': 0.07; 'variable,': 0.07; 'created,': 0.09; 'fetch': 0.09; 'newly': 0.09; 'received :mail-qc0-f174.google.com': 0.09; 'variables,': 0.09; 'def': 0.10; 'value.': 0.15; '[2,': 0.16; 'created.': 0.16; 'funcs': 0.16; 'lambda': 0.16; 'creates': 0.18; 'variable': 0.20; 'regardless': 0.21; 'precise': 0.22; 'stick': 0.22; 'cheers,': 0.23; 'needed.': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.25; 'message- id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'subject:list': 0.28; 'environment': 0.29; 'once,': 0.29; 'case,': 0.29; "i'm": 0.29; 'daniel': 0.30; 'function': 0.30; 'point': 0.31; 'not.': 0.32; 'print': 0.32; 'asked': 0.33; 'environment,': 0.33; 'instead,': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'wrong': 0.34; 'list': 0.35; 'from:addr:googlemail.com': 0.35; 'sequence': 0.35; 'stores': 0.35; 'doing': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'created': 0.36; 'but': 0.36; 'should': 0.36; 'thank': 0.36; 'enough': 0.36; 'two': 0.37; 'received:209': 0.37; 'received:209.85.216': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'store': 0.38; 'object': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'little': 0.39; 'header:Received:5': 0.40; 'easy': 0.60; 'most': 0.61; 'solve': 0.62; 'different': 0.63; 'surprise': 0.65; 'obvious': 0.71; 'down!': 0.84; 'to:name:python': 0.84; 'subject:funny': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=V4wOV06uJgc7oeV1RGSTDxb/mYSPyOlqZET5COFoyq0=; b=Tr1A7riFfwx77gu2njCOa3Gl/vEAPmMim3hc3TFVVBLVIj089fNFvZo2tL/thfQ3bk L4YRP6cFXCIaiBAJ7cewxUDskwPBRTif1/Tki2KeEwSSQI3hLdP0FTS7y5m11g7pJidZ ux1pTrnupfUvsDEkVRiJLTJrhlNAKXmP16aKaOYiR42YaVAC4B+FfWZ8p/N1SKcaa8H0 KTsLr5DExLEkLiQdjq7F0aQqPEboClN20UIjFFkd9gpxhP9UeDI6KWM4oc34VSFQit8/ YM0kRTnzJ+o2MzPo6lJhuRJlVTtoAY6t69bFwkeuli1qAl67+6POCZMgv9LhuIPL8hWT 1LDw== |
| MIME-Version | 1.0 |
| In-Reply-To | <4ffe1096$0$29965$c3e8da3$5496439d@news.astraweb.com> |
| References | <mailman.2006.1341988919.4697.python-list@python.org> <d390ef92-5316-422b-b180-5a944d33fd68@d6g2000pbt.googlegroups.com> <4ffe1096$0$29965$c3e8da3$5496439d@news.astraweb.com> |
| Date | Thu, 12 Jul 2012 04:54:32 +0200 |
| Subject | Re: lambda in list comprehension acting funny |
| From | Daniel Fetchinson <fetchinson@googlemail.com> |
| To | Python <python-list@python.org> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2021.1342061675.4697.python-list@python.org> (permalink) |
| Lines | 72 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1342061675 news.xs4all.nl 6899 [2001:888:2000:d::a6]:50963 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:25189 |
Show key headers only | View raw
>> You should not be using lambda in this case >> .for x in [2, 3]: >> . funcs = [x**ctr for ctr in range( 5 )] >> . for p in range(5): >> . print x, funcs[p] >> . print > > If you change the requirements, it's always easy to solve problems. But > it is the wrong problem that you have solved. > > The problem we have been asked to solve is to create a sequence of > function objects, so that they can be called later, when needed, *not* to > pre-calculate the results. > > In this case, the most obvious solution is to store a local variable in > each function object with the value you want. > > funcs = [(lambda x, i=i: x**i) for i in range(5)] > > creates a list of five functions: > > lambda x, i=0: x**i > lambda x, i=1: x**i > lambda x, i=2: x**i > and so on. > > In this case, each function has two local variables, x and i, with i > having a default value. The function parameter i is bound to the value of > i when the function was created. > > Because parameter defaults are calculated once, when the function is > created, this causes the value of i to stick to the newly created > function, and we get the result we need. > > What happens if you don't use a parameter with a default value? > > funcs = [(lambda x: x**i) for i in range(5)] > > In this case, each function body contains one local variable, x, and one > non-local or global variable, i. > > Because i is a non-local, the function doesn't store a value for it. > Instead, the function stores a lump of data pointing to just enough of > the environment to fetch the current value of the non-local i when > needed. Since all five functions are in the same environment, they all > see the same value of i when you call them, regardless of what the value > of i was when they were created. > > This is little different from doing this: > > i = 1 > def f1(x): return x**i > > i = 2 > def f2(x): return x**i > > i = 3 > def f3(x): return x**i > > Is there any surprise that all three functions return the same value? > They all point to the same global variable i. I'm not sure what it is > about lambda that fools people into thinking that it is different (I've > even been fooled myself!) but it is not. Thank you Steve! Precise and clear, as always! Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
lambda in list comprehension acting funny Daniel Fetchinson <fetchinson@googlemail.com> - 2012-07-11 08:41 +0200
Re: lambda in list comprehension acting funny "Colin J. Williams" <cjw@ncf.ca> - 2012-07-11 06:28 -0400
Re: lambda in list comprehension acting funny Ian Kelly <ian.g.kelly@gmail.com> - 2012-07-11 10:34 -0600
Re: lambda in list comprehension acting funny 88888 Dihedral <dihedral88888@googlemail.com> - 2012-07-11 20:39 -0700
Re: lambda in list comprehension acting funny Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-12 03:51 +0000
Re: lambda in list comprehension acting funny 88888 Dihedral <dihedral88888@googlemail.com> - 2012-07-11 22:04 -0700
Re: lambda in list comprehension acting funny Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-12 06:18 +0000
Re: lambda in list comprehension acting funny Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-07-12 08:40 +0100
Re: lambda in list comprehension acting funny 88888 Dihedral <dihedral88888@googlemail.com> - 2012-07-11 20:39 -0700
Re: lambda in list comprehension acting funny woooee <woooee@gmail.com> - 2012-07-11 11:38 -0700
Re: lambda in list comprehension acting funny John Ladasky <john_ladasky@sbcglobal.net> - 2012-07-11 13:21 -0700
Re: lambda in list comprehension acting funny Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-12 00:52 +0000
Re: lambda in list comprehension acting funny Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-07-11 21:05 -0400
Re: lambda in list comprehension acting funny Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-12 03:53 +0000
Re: lambda in list comprehension acting funny Terry Reedy <tjreedy@udel.edu> - 2012-07-12 00:24 -0400
Re: lambda in list comprehension acting funny Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-07-12 00:39 -0400
Re: lambda in list comprehension acting funny Robert Miles <robertmiles@teranews.com> - 2012-08-15 19:26 -0500
Re: lambda in list comprehension acting funny John O'Hagan <research@johnohagan.com> - 2012-07-12 15:29 +1000
Re: lambda in list comprehension acting funny Robert Kern <robert.kern@gmail.com> - 2012-07-12 10:06 +0100
Re: lambda in list comprehension acting funny Hans Mulder <hansmu@xs4all.nl> - 2012-07-12 00:22 +0200
Re: lambda in list comprehension acting funny Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-11 23:47 +0000
Re: lambda in list comprehension acting funny Daniel Fetchinson <fetchinson@googlemail.com> - 2012-07-12 04:54 +0200
Re: lambda in list comprehension acting funny Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-12 03:59 +0000
Re: lambda in list comprehension acting funny Ian Kelly <ian.g.kelly@gmail.com> - 2012-07-12 10:53 -0600
Re: lambda in list comprehension acting funny Rotwang <sg552@hotmail.co.uk> - 2012-07-12 18:23 +0100
Re: lambda in list comprehension acting funny Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-13 02:20 +0000
Re: lambda in list comprehension acting funny rusi <rustompmody@gmail.com> - 2012-07-12 21:33 -0700
Re: lambda in list comprehension acting funny Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-13 06:36 +0000
Re: lambda in list comprehension acting funny rusi <rustompmody@gmail.com> - 2012-07-13 06:44 -0700
Re: lambda in list comprehension acting funny rusi <rustompmody@gmail.com> - 2012-07-13 07:45 -0700
RE: lambda in list comprehension acting funny "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-07-13 16:12 +0000
Re: lambda in list comprehension acting funny rusi <rustompmody@gmail.com> - 2012-07-13 09:46 -0700
Re: lambda in list comprehension acting funny Chris Angelico <rosuav@gmail.com> - 2012-07-14 03:20 +1000
Re: lambda in list comprehension acting funny Hans Mulder <hansmu@xs4all.nl> - 2012-07-13 19:53 +0200
RE: lambda in list comprehension acting funny "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-07-13 18:06 +0000
Re: lambda in list comprehension acting funny Ian Kelly <ian.g.kelly@gmail.com> - 2012-07-13 12:54 -0600
Re: lambda in list comprehension acting funny Hans Mulder <hansmu@xs4all.nl> - 2012-07-13 21:26 +0200
Re: lambda in list comprehension acting funny Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-14 23:29 +0000
Re: lambda in list comprehension acting funny Chris Angelico <rosuav@gmail.com> - 2012-07-15 10:49 +1000
Re: lambda in list comprehension acting funny Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-15 08:32 +0000
Re: lambda in list comprehension acting funny Chris Angelico <rosuav@gmail.com> - 2012-07-15 18:44 +1000
Re: lambda in list comprehension acting funny Hans Mulder <hansmu@xs4all.nl> - 2012-07-15 21:25 +0200
Re: lambda in list comprehension acting funny Terry Reedy <tjreedy@udel.edu> - 2012-07-15 06:27 -0400
Re: lambda in list comprehension acting funny rusi <rustompmody@gmail.com> - 2012-07-13 19:31 -0700
Re: lambda in list comprehension acting funny Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-14 03:43 +0000
Re: lambda in list comprehension acting funny rusi <rustompmody@gmail.com> - 2012-07-13 21:53 -0700
Re: lambda in list comprehension acting funny Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-14 07:46 +0000
Re: lambda in list comprehension acting funny Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-07-13 13:47 -0400
csiph-web