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


Groups > comp.lang.python > #99020

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

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: Could you explain why the following generates 4 same elements list?
Date Wed, 18 Nov 2015 17:47:51 -0700
Lines 23
Message-ID <mailman.436.1447894114.16136.python-list@python.org> (permalink)
References <3f1eecc8-e23d-4f86-abf8-38044939d085@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de eGZRYg80NPMeIHRV5mFIBwqwgiBwuYJSs58C21IOPjeQ==
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.011
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'received:209.85.223': 0.03; 'modified': 0.05; 'fails.': 0.09; 'subject:same': 0.09; 'subject:why': 0.09; 'def': 0.13; 'wed,': 0.15; 'received:io': 0.16; 'received:psf.io': 0.16; 'result:': 0.16; 'subject:Could': 0.16; 'url:faq': 0.16; 'wrote:': 0.16; '2015': 0.20; 'work,': 0.21; 'code.': 0.23; 'tried': 0.24; 'header:In-Reply-To:1': 0.24; 'subject:list': 0.26; 'message-id:@mail.gmail.com': 0.27; 'code:': 0.29; 'print': 0.30; 'code': 0.30; 'url:python': 0.33; 'received:google.com': 0.35; 'could': 0.35; 'nov': 0.35; 'but': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'form,': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'hi,': 0.38; 'why': 0.39; 'does': 0.39; 'subject:the': 0.39; 'to:addr:python.org': 0.40; 'easy': 0.60; 'url:3': 0.60; 'skip:u 10': 0.61; 'show': 0.62; 'to:name:python': 0.84; 'url:programming': 0.84; 'subject:you': 0.85
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=XywgjUj3aJ2mWQ78VS44vwCkvdqupIWIqDHQ4m0tFec=; b=YIVw8j+Yn5fL87HANf6mHhOhQnTJVnOOJHQyDhf4Na8a7vO9nPBstDPEa1MlPGwuQG SWRhTo5Yk2dswAW1B4Sf6OkLDL+poncMpTogL+qFJVfR90i3EOU6xbpkUMkDQSMIOSZG 2CKDC5OjY/1uKIFbZmoiLlqvC/IW61/RWr0hiTob0w7bz37uLIbthppvPlXsuZ5aCwzu EhDtmQ5j65HXjitnIQ6CZ5gyDmIvb6hcuC3AcquTgJi6HLGJ9TerDPFwPe2y8qrRP2Se 0i/kgUrrsmcsdKR+2B265SU37eXqVcjJUHVqsINmH1pgivRxcEk7FpO39S/KLlQKcNXZ 1zNg==
X-Received by 10.107.164.154 with SMTP id d26mr5910961ioj.111.1447894110467; Wed, 18 Nov 2015 16:48:30 -0800 (PST)
In-Reply-To <3f1eecc8-e23d-4f86-abf8-38044939d085@googlegroups.com>
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:99020

Show key headers only | View raw


On Wed, Nov 18, 2015 at 5:05 PM, fl <rxjwg98@gmail.com> wrote:
> 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]
>
>
> I have tried to simplify the above code to an easy understanding form,
> but fails. Either the modified code does not work, or it does not show
> relation to the original code.
>
> Could you explore it a little for me to understand it easier?

https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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