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


Groups > comp.lang.python > #54477

lambda - strange behavior

From Kasper Guldmann <gmane@kalleguld.dk>
Subject lambda - strange behavior
Date 2013-09-20 15:21 +0000
Newsgroups comp.lang.python
Message-ID <mailman.185.1379690708.18130.python-list@python.org> (permalink)

Show all headers | View raw


I was playing around with lambda functions, but I cannot seem to fully grasp
them. I was running the script below in Python 2.7.5, and it doesn't do what
I want it to. Are lambda functions really supposed to work that way. How do
I make it work as I intend?

f = []
for n in range(5):
    f.append( lambda x: x*n )

assert( f[4](2) == 8 )
assert( f[3](3) == 9 )
assert( f[2](2) == 4 )
assert( f[1](8) == 8 )
assert( f[0](2) == 0 )

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


Thread

lambda - strange behavior Kasper Guldmann <gmane@kalleguld.dk> - 2013-09-20 15:21 +0000
  Re: lambda - strange behavior Rotwang <sg552@hotmail.co.uk> - 2013-09-20 16:52 +0100
  Re: lambda - strange behavior rusi <rustompmody@gmail.com> - 2013-09-20 09:25 -0700
  Re: lambda - strange behavior Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-09-20 19:30 +0300

csiph-web