why any( ) instead of firsttrue( ) ?

Ian Kelly ian.g.kelly at gmail.com
Tue Jun 8 18:24:01 EDT 2010


On Tue, Jun 8, 2010 at 3:16 PM, danieldelay <danieldelay at gmail.com> wrote:
> This function "firsttrue( )" could probably be used anywhere "any( )" is
> used, but with the ability to retrieve the first element where bool(element)
> is True, which may be sometimes usefull.
>
> I suppose that there is a reason for that, does anybody know it ?

Because it was designed as a replacement for "reduce(lambda x, y: x or
y, iterable)".  The problem arises when the iterable is empty.  What
false value should be returned?  If the iterable is a sequence of
bools, then None doesn't fit.  If the iterable is a sequence of
non-bool objects, then False doesn't fit.  In the case of reduce, the
problem is solved by explicitly specifying an initial value to be used
when the sequence is empty, but I guess GVR didn't feel that was
appropriate here.

Cheers,
Ian



More information about the Python-list mailing list