why any( ) instead of firsttrue( ) ?

MRAB python at mrabarnett.plus.com
Tue Jun 8 19:18:29 EDT 2010


danieldelay wrote:
> Le 09/06/2010 00:24, Ian Kelly a écrit :
>> 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
> 
> Thanks for your reply, it helps me to understand this choice wether I do 
> not agree with it.
> 
> "False" sounds natural for a function called "any()" which makes a 
> boolean calculus
> 
> "None" sounds natural for a function called "firsttrue()" which tries to 
> retrieve an element.
> 
> As the two make sense, I would have chosen the "firsttrue()" which is 
> more powerfull...
> 
> Perhaps "any()" whas choosen to keep a beautiful symmetry with "all()", 
> that I can interprete only as a boolean calculus.
> 
> Does GVR prefers beauty to power ?
> 
> firsttrue(line.strip() for line in '\n\n \n CHEERS  \n'.split('\n'))
> 
Should 'firsttrue' return None? Surely, if none are true then it should
raise an exception.



More information about the Python-list mailing list