Supply condition in function call
Grant Edwards
invalid at invalid.invalid
Wed Mar 25 15:53:16 EDT 2015
On 2015-03-25, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Wed, Mar 25, 2015 at 11:29 AM, Manuel Graune <manuel.graune at koeln.de> 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.
--
Grant Edwards grant.b.edwards Yow! I'm RELIGIOUS!!
at I love a man with
gmail.com a HAIRPIECE!! Equip me
with MISSILES!!
More information about the Python-list
mailing list