Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 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; 'python': 0.11; 'grasp': 0.16; 'lambda': 0.16; 'message-id:@post.gmane.org': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'header:User-Agent:1': 0.23; 'script': 0.25; 'header:X-Complaints-To:1': 0.27; "doesn't": 0.30; 'supposed': 0.32; 'running': 0.33; 'received:84': 0.35; 'but': 0.35; 'really': 0.36; 'charset:us-ascii': 0.36; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Kasper Guldmann Subject: lambda - strange behavior Date: Fri, 20 Sep 2013 15:21:20 +0000 (UTC) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: sea.gmane.org User-Agent: Loom/3.14 (http://gmane.org/) X-Loom-IP: 84.238.70.61 (Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 15 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1379690708 news.xs4all.nl 15892 [2001:888:2000:d::a6]:56784 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54477 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 )