[Python-ideas] Adding an optional function argument to all() and any() builtins

Rob Cliffe rob.cliffe at btinternet.com
Sun Nov 21 23:43:25 CET 2010



On 21/11/2010 22:03, Chris Rebert wrote:
> On Sun, Nov 21, 2010 at 1:35 PM, Masklinn<masklinn at masklinn.net>  wrote:
>> On 2010-11-21, at 21:43 , Alexander Belopolsky wrote:
>>> On Sun, Nov 21, 2010 at 3:20 PM, Masklinn<masklinn at masklinn.net>  wrote:
>>>> On 2010-11-21, at 21:11 , MRAB wrote:
>>>>> if any(mytestfunction(i) for i in mylist):
>>>>>     foo()
>>>> is not an improvement over `if any(map(mytestfunction, mylist))` or `if any(imap(mytestfunction, mylist))`
>>> In 2.7, both MRAB's generator expression solution and your imap() are
>>> improvements over map() because they don't create a temporary list.
>> Which doesn't actually mean it's an improvement, gencomps are not always faster than map when the transformer is a function in all cases.
> In many cases it will be though, because the gencomp is not eagerly
> evaluated, unlike listcomps and old map()s. gencomps are also
> definitely more memory efficient.
>
> Cheers,
> Chris
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
For me,

     if any(mytestfunction(i) for i in mylist):
         foo()

wins hands down.  Not only is it, I would guess, quite efficient, but it 
is immediately readable, in particular to newbies.*  Whereas the 
readability of
     if any(map(mytestfunction, mylist))
depends on how familiar you are with map().  If you've never heard of 
it, it's quite impenetrable, as the name "map" is not IMHO very 
meaningful.  (Not that I can think of anything better to call it, or 
that there is any chance of it being renamed.  Still ... perhaps 
"apply_all" or some suc?)

[* I hope I can be forgiven for speaking on behalf of newbies, when I 
don't - quite - class myself as one any more.]

Best wishes
Rob Cliffe



More information about the Python-ideas mailing list