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


Groups > comp.lang.python > #99033

Re: Could you explain why the following generates 4 same elements list?

Path csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!not-for-mail
From dieter <dieter@handshake.de>
Newsgroups comp.lang.python
Subject Re: Could you explain why the following generates 4 same elements list?
Date Thu, 19 Nov 2015 08:27:52 +0100
Lines 27
Message-ID <mailman.445.1447918081.16136.python-list@python.org> (permalink)
References <3f1eecc8-e23d-4f86-abf8-38044939d085@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
X-Trace news.uni-berlin.de n0lWAYYIw4METp3fTFjfsA2KLrFp8zv+eAnwjAfnRZQA==
Cancel-Lock sha1:sGXxjJ45ZelmfOwJSMiKM590FH0=
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:same': 0.09; 'subject:why': 0.09; 'def': 0.13; 'lambda': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'result:': 0.16; 'subject:Could': 0.16; 'variable': 0.18; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; '(which': 0.26; 'header:X-Complaints-To:1': 0.26; 'thus,': 0.29; 'value)': 0.29; 'code:': 0.29; 'print': 0.30; 'definition': 0.34; 'could': 0.35; 'replaced': 0.35; 'but': 0.36; '(i.e.': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'hi,': 0.38; 'why': 0.39; 'subject:the': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'called': 0.40; 'your': 0.60; 'avoid': 0.61; 'received:217': 0.66; 'subject:you': 0.85
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host pd9e09b8f.dip0.t-ipconnect.de
User-Agent Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:99033

Show key headers only | View raw


fl <rxjwg98@gmail.com> writes:

> Hi,
>
> I cannot reason out why the code:
> ////////
> def mpl():
>     return [lambda x : i * x for i in range(4)]
>     
> print [m(2) for m in mpl()]
> /////////
>
> has result:
>
> [6, 6, 6, 6]

The "i" in your lambda definition is a variable reference which
is not dereferenced (i.e. name replaced by the value) at definition
but only at call time. Thus, all your "lambda"s are in fact equal;
they all look up the current value of "i" when they are called
(which happens to be "3").

To avoid this, you must force the dereferencing at definition time.
This could look like:

     return [lambda x, i=i: i * x for i in range(4)]

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


Thread

Could you explain why the following generates 4 same elements list? fl <rxjwg98@gmail.com> - 2015-11-18 16:05 -0800
  Re: Could you explain why the following generates 4 same elements list? Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-18 17:47 -0700
  Re: Could you explain why the following generates 4 same elements list? dieter <dieter@handshake.de> - 2015-11-19 08:27 +0100

csiph-web