why any( ) instead of firsttrue( ) ?

danieldelay danieldelay at gmail.com
Tue Jun 8 17:16:01 EDT 2010


Hi,

I find very useful in python the ability to use a list or number x like 
a boolean :

    if x :
       do something


So I don't understand why was introduced the any( ) function defined as :

   def any(iterable):
     for element in iterable:
         if element:
             return True
     return False

instead of a function firsttrue( ) that could have been defined as :

   def firsttrue(iterable):
     for element in iterable:
         if element:
             return element
     return None

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 ?

Daniel



More information about the Python-list mailing list