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

Chris Rebert pyideas at rebertia.com
Mon Nov 22 01:35:45 CET 2010

On Sun, Nov 21, 2010 at 3:40 PM, Carl M. Johnson
<cmjohnson.mailinglist at gmail.com> wrote:
> On Sun, Nov 21, 2010 at 1:12 PM, Steven D'Aprano wrote:
>> When you have difficulty thinking of a name which clearly and concisely
>> describes what the argument does, that's often a good hint that you are
>> dumping too much responsibility onto the function.
>> Having said that, I'd suggest that an appropriate name might be the same
>> name used by sort and friends: key.
> I agree with almost everything you wrote, except the suggestion of "key". In
> sorted et al., the point of key is that you want a certain form used when
> comparing, but *not* the sorted list that is the final product. That's why
> you can't just use sorted(name.lower() for name in names) as a substitute
> for sorted(names, key=str.lower). Compare the following:
>>>> sorted( [u"MacDonald", u"Macan"])
> [u'MacDonald', u'Macan']
>>>> sorted(s.lower() for s in [u"MacDonald", u"Macan"])
> [u'macan', u'macdonald']
>>>> sorted([u"MacDonald", u"Macan"], key=unicode.lower)
> [u'Macan', u'MacDonald']
> None of this applies to any/all, since they just return a True or a False,
> no matter what kind of iterable input they get. So, key is not a good name,
> and your earlier analysis of why there shouldn't be another keyword on
> any/all stands.

-1 on adding such a keyword, for the reasons already brought up; but
just to play Devil's Advocate, "predicate" (or perhaps "pred" or even
"p" depending on how terse you want to be) would be a good name for
the hypothetical parameter. Because one is testing whether any() or
all() of the items in a collection satisfy a given predicate (in the
trivial case, the "identity" bool() predicate).


More information about the Python-ideas mailing list