Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.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.070 X-Spam-Evidence: '*H*': 0.87; '*S*': 0.00; '21,': 0.07; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'happily': 0.16; 'instead:': 0.16; 'kasper': 0.16; 'lambda': 0.16; 'scope,': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; 'too.': 0.31; 'sep': 0.31; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'two': 0.37; 'to:addr :python-list': 0.38; 'list,': 0.38; 'to:addr:python.org': 0.39; 'free': 0.61; "you're": 0.61; 'within': 0.65; 'reads': 0.68; 'default': 0.69; 'technically': 0.84; 'do:': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=x+ber+GAC45fe8epDbDcvhXN2lDODSy9qHv6iGSEJBY=; b=c+Q6kOo/wc6cbL4NlnL76IDqWuQ7uHhoZmUF8OHlD1PknuNWBpLURgqBhCDsFVWHTQ 7WlzAgJO8V5sbvpOE1HCW4AOucs23HMnX8t1Pp8lRuILp7S9SOlepBkmSriRNfg3C1Wa rP/NlOUrjn0XmfZVP8L8vUXP/c+KMjRuJoSF9uYKLWWGE1nOUhvC7NhLBeeT6oxKKqdo 6KskHVR74UWJrS3ftVyCnXV3/YDPvJ22filoYajbA6bFSs8CNm14X7CEeYOCn8HXX85f Uzw1f0vQz/A9j8eHvdieR65gzqiBaIopKawBN3VoaKeLIOtyyNtXDdYGJeTb2GaSYOMt 2rhw== MIME-Version: 1.0 X-Received: by 10.52.228.137 with SMTP id si9mr1016226vdc.29.1379692070885; Fri, 20 Sep 2013 08:47:50 -0700 (PDT) In-Reply-To: References: Date: Sat, 21 Sep 2013 01:47:50 +1000 Subject: Re: lambda - strange behavior From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1379692080 news.xs4all.nl 15976 [2001:888:2000:d::a6]:35045 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54480 On Sat, Sep 21, 2013 at 1:21 AM, Kasper Guldmann wrote: > f = [] > for n in range(5): > f.append( lambda x: x*n ) You're leaving n as a free variable here. The lambda function will happily look to an enclosing scope, so this doesn't work. But there is a neat trick you can do: f = [] for n in range(5): f.append( lambda x,n=n: x*n ) You declare n as a second parameter, with a default value of the current n. The two n's are technically completely separate, and one of them is bound within the lambda. BTW, any time you have a loop appending to a list, see if you can make it a comprehension instead: f = [lambda x,n=n: x*n for n in range(5)] Often reads better, may execute better too. ChrisA