Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99016 > unrolled thread
| Started by | fl <rxjwg98@gmail.com> |
|---|---|
| First post | 2015-11-18 16:05 -0800 |
| Last post | 2015-11-19 08:27 +0100 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | fl <rxjwg98@gmail.com> |
|---|---|
| Date | 2015-11-18 16:05 -0800 |
| Subject | Could you explain why the following generates 4 same elements list? |
| Message-ID | <3f1eecc8-e23d-4f86-abf8-38044939d085@googlegroups.com> |
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?
Thanks in advance.
[toc] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2015-11-18 17:47 -0700 |
| Message-ID | <mailman.436.1447894114.16136.python-list@python.org> |
| In reply to | #99016 |
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
[toc] | [prev] | [next] | [standalone]
| From | dieter <dieter@handshake.de> |
|---|---|
| Date | 2015-11-19 08:27 +0100 |
| Message-ID | <mailman.445.1447918081.16136.python-list@python.org> |
| In reply to | #99016 |
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)]
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web