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


Groups > comp.lang.python > #99016

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

Newsgroups comp.lang.python
Date 2015-11-18 16:05 -0800
Message-ID <3f1eecc8-e23d-4f86-abf8-38044939d085@googlegroups.com> (permalink)
Subject Could you explain why the following generates 4 same elements list?
From fl <rxjwg98@gmail.com>

Show all headers | View raw


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.

Back to comp.lang.python | Previous | NextNext 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