Re: replace 'apply' with extended call

From: Alan G Isaac (aisaac_at_american.edu)
Date: 10/25/04


Date: Mon, 25 Oct 2004 10:23:12 -0400


"Andrew Dalke" <adalke@mindspring.com> wrote in message
news:b61fd.1486$kM.997@newsread3.news.pas.earthlink.net...
> How about a solution which replaces the 'map' with a
> list comprehension?
> def apply_each(fns, args = []):
> return [fn(*args) for fn in fns]
> Conversion to lambda form is trivial for this case but I
> figured if you're going to name it, why use a lambda?

This raises another (newbie) question that I had.
Take a trivial example:

from operator import truth
bool1 = lambda lst: map(truth, lst)
def bool2(lst): return map(truth,lst)
def bool3(lst): return [truth(_) for _ in lst]

To my eyes, the most natural is bool2.
I would never have considered bool1 if
I had not come across it in the Merz book,
but it is both shortest and clear.
I include bool3 just for comparison: I think
the way in which it is harder to read illustrates
the usefulness of 'map'.

So, are there any obvious considerations when
making a choice among these. In particular,
why might someone prefer the style in bool1?

Thanks,
Alan Isaac



Relevant Pages