[Python-ideas] Provide a 'key' argument for all() and any() builtins

Giampaolo Rodolà g.rodola at gmail.com
Mon Jul 23 06:03:24 CEST 2012


This would be similar to 'key' argument already available for min(), max()
and sorted() and would let user decide what must be considered True and
what not.
Some use cases:


all(a, b, c, ... key=callable)
all(a, b, c, ... key=lambda x: isinstance(x, int))
any(a, b, c, ... key=lambda x: x < 10)

a = b = c = None
for x in foo:
    if cond1:
         a = 0
    elif cond2:
         b = 0
    elif cond3:
         c = 0
# meaning all cond* has been satisfied and it's ok to consider 0 a
meaningful value
assert all(a, b, c, key=lambda x: x != None)


Thoughts?

--- Giampaolo
http://code.google.com/p/pyftpdlib/
http://code.google.com/p/psutil/
http://code.google.com/p/pysendfile/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120723/530d1aaf/attachment.html>


More information about the Python-ideas mailing list