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

Carl M. Johnson cmjohnson.mailinglist at gmail.com
Mon Nov 22 00:40:46 CET 2010

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.

-- Carl Johnson
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20101121/b8a54284/attachment.html>

More information about the Python-ideas mailing list