Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!nntp.club.cc.cmu.edu!micro-heart-of-gold.mit.edu!bloom-beacon.mit.edu!bloom-beacon.mit.edu!panix!not-for-mail From: Grant Edwards Newsgroups: comp.lang.python Subject: Re: Supply condition in function call Date: Thu, 26 Mar 2015 15:41:22 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 42 Message-ID: References: <87bnjhyohp.fsf@uriel.graune.org> NNTP-Posting-Host: 67-130-15-94.dia.static.qwest.net X-Trace: reader1.panix.com 1427384482 19014 67.130.15.94 (26 Mar 2015 15:41:22 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Thu, 26 Mar 2015 15:41:22 +0000 (UTC) User-Agent: slrn/1.0.1 (Linux) Xref: csiph.com comp.lang.python:88070 On 2015-03-25, Ian Kelly wrote: > On Wed, Mar 25, 2015 at 1:53 PM, Grant Edwards wrote: >> On 2015-03-25, Ian Kelly wrote: >>> On Wed, Mar 25, 2015 at 11:29 AM, Manuel Graune wrote: >>> >>>> I'm looking for a way to supply a condition to an if-statement inside a >>>> function body when calling the function. I can sort of get what I want >>>> with using eval [...] >>> >>> Pass the condition as a function. >>> >>> def test1(a, b, condition=lambda i, j: True): >>> for i,j in zip(a,b): >>> c=i+j >>> if condition(i, j): >>> print("Foo") >>> >>> test1([0,1,2,3],[1,2,3,4], lambda i, j: i+j > 4) >>> # etc. >> >> FWIW, back in the day such a function was referred to as a "thunk" >> (particularly if it was auto-generated by a compiler that used >> pass-by-name instead of pass-by-value or pass-by-reference): >> >> http://en.wikipedia.org/wiki/Thunk >> >> Dunno if people still use that term or not. > > I've heard the term (though not since my college days, I think), but > I've always understood thunks to be parameterless (hence the use as a > form of pass-by-name). You're right -- I misread the example. Somehow I skipped the "for i,j" line completely, and was thinking that i and j were defined in the caller's context. Thus the OP was trying to implment something akin to call by name. -- Grant Edwards grant.b.edwards Yow! Here I am in the at POSTERIOR OLFACTORY LOBULE gmail.com but I don't see CARL SAGAN anywhere!!