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


Groups > comp.lang.python > #25169

Re: lambda in list comprehension acting funny

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'expressions': 0.07; 'parameter': 0.07; 'subject:skip:c 10': 0.07; 'variant': 0.07; 'python': 0.09; 'references.': 0.09; 'stored': 0.10; 'def': 0.10; "'at": 0.16; 'bind': 0.16; 'funcs': 0.16; 'lambda': 0.16; 'lot!': 0.16; 'received:209.85.216.53': 0.16; 'simplest': 0.16; '>>>': 0.18; 'variable': 0.20; 'cheers,': 0.23; 'references': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.25; 'message- id:@mail.gmail.com': 0.27; 'subject:list': 0.28; 'worked': 0.30; 'daniel': 0.30; 'print': 0.32; 'anyone': 0.33; 'to:addr:python- list': 0.33; 'received:google.com': 0.34; 'thanks': 0.34; 'from:addr:googlemail.com': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'does': 0.37; 'received:209': 0.37; 'received:209.85.216': 0.37; 'subject:: ': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'more': 0.63; 'importantly,': 0.65; '(they': 0.84; 'down!': 0.84; 'to:name:python': 0.84; 'why?': 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=S+aHgNFMkoVAmmR5Nr0RMjKFg6ae+HEmFAnoD3EPl2U=; b=OdK+tleojscn5slvsxPg5Trowpg/OKgGcRqh450W+9SUzwdFzl0vFzC6EuYE8CaSGT 91/TU1kmsHZhdixEBcFnN9OFPqK81JPZIxwG0jqPtq7CYDNOEiNZflY/2AxgmFQ24wp2 EQTUv1OlQCtYaKzOthke9xH/qOMJIfWp/1+K0PmtSZtQTKD+nHn4+YlkZHUqENc/P/DS BvFHw1w7HVuYqSRukvfwWmAaX9Nh12QVqEuaBHZ5MRoUQDOAJPOcE4CRxUXDwt4z8Pa/ NN17nrgYTntAqlUPU5pqny6Yq88Fb07AflSMawCieVeuil3wMVt0BX29K+ePUQLDGbiE twXQ==
MIME-Version 1.0
In-Reply-To <jtj8bt$rl4$1@dough.gmane.org>
References <CADjSo4QvTnGKy_ZYOO5qZu3tSKovwAMGjku4L+RXEkYmwXPz4g@mail.gmail.com> <CADjSo4SeuMJOP_iEM9rjVsFtVh84HaVUtPkUCZGKiEdQBWuerw@mail.gmail.com> <jtj8bt$rl4$1@dough.gmane.org>
Date Wed, 11 Jul 2012 09:36:14 +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.2009.1341992176.4697.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1341992176 news.xs4all.nl 6880 [2001:888:2000:d::a6]:45432
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:25169

Show key headers only | View raw


>>> funcs = [ lambda x: x**i for i in range( 5 ) ]
>>> print funcs[0]( 2 )
>>>
>>> This gives me
>>> 16
>>>
>>> When I was excepting
>>> 1
>>>
>>> Does anyone know why?
>
>    Just the way Python lambda expressions bind their variable
> references. Inner 'i' references the outer scope's 'i' variable and not
> its value 'at the time the lambda got defined'.
>
>
>> And more importantly, what's the simplest way to achieve the latter? :)
>
>    Try giving the lambda a default parameter (they get calculated and
> have their value stored at the time the lambda is defined) like this:
>    funcs = [ lambda x, i=i: x**i for i in range( 5 ) ]

Thanks a lot!
I worked around it by

def p(i):
    return lambda x: x**i
funcs = [ p(i) for i in range(5) ]

But your variant is nicer (matter of taste of course).

Cheers,
Daniel


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown

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


Thread

Re: lambda in list comprehension acting funny Daniel Fetchinson <fetchinson@googlemail.com> - 2012-07-11 09:36 +0200

csiph-web