Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx02.iad01.newshosting.com!newshosting.com!69.16.185.21.MISMATCH!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe04.iad.POSTED!00000000!not-for-mail From: harrismh777 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.16) Gecko/20101206 SeaMonkey/2.0.11 MIME-Version: 1.0 Newsgroups: comp.lang.python Subject: Re: Something is rotten in Denmark... References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Lines: 42 Message-ID: NNTP-Posting-Host: niopnfljcojmkdanmfjemddihkedgbga X-Complaints-To: abuse@charter.net X-Trace: ifnckfofmjglplgfffhhjjcnmknpkahnolmankdmfnhicncjniopnfljcojmkdanokgaginhicojjhebildecjgkneamafapmcficffcjghoaklnjgoiemlkkopjlhpeplepgefcpbbnokgh NNTP-Posting-Date: Tue, 31 May 2011 20:18:37 UTC Date: Tue, 31 May 2011 15:18:37 -0500 Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6755 Terry Reedy wrote: > You have been hypnotizeed by lambda. (lambda n: i+n) is a *constant > expression*, so you get 10 'equal' functions. 'hypnotized' indeed! ... ok, so let me see if I get this... the lambda defers lookup|bind of its references until such time as the lambda is 'called' and not at the time (as I thought) that the anonymous function(s) are returned? If I'm understanding that correctly, then that means lambda is working as designed, and that there are very subtle nuances to be aware of. In my little case (lambda n: i + n) ... if the i goes out of scope before the anonymous function gets called then we have a problem... or if i as a reference is mutable or refers to a different object before the anonymous function is called then we have a problem? What I am discovering is that 'yes' I can use lambda syntactically where I might not be able to code a def statement; however, if I do use it (as in a list comprehension) then I may get unexpected results if any of my lambda's references go out of scope or are mutable...(?) Question: What are the ramifications of making the lookup|binding happen at the time the anonymous function is 'returned' vs 'called'? Has anyone suggested this? Is this PEP-able....? Are there side-effects in the other direction? PS Thanks Chris, Ian, Jussi, Thomas, Terry... I appreciate your teaching and patience ! kind regards, m harris