Should any() and all() take a key= argument?
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Sat Apr 1 05:44:20 EST 2006
On Fri, 31 Mar 2006 22:35:10 -0800, Steve R. Hastings wrote:
> The list.sort() method accepts a "key=" parameter to let you specify a
> function that will change the way it sorts. In Python 2.5, min() and
> max() now accept a "key=" parameter that changes how the functions decide
> min or max.
>
> Should any() and all() take a key= argument?
>
> Example:
>
>>>> lst = [2, 4, 42]
>>>> any(lst, key=lambda x: x == 42)
> True
In my opinion, that's an abuse of the term "key".
Here's another way of doing it:
lst = [2, 4, 42]
any(map(lambda x: x==42, lst))
> I kind of like the key= option. The need isn't as strong as with
> .sort(), min(), and max(), but consistency can be a good thing. I'd
> personally like to see key= anywhere it makes sense.
This isn't one of those places.
--
Steven.
More information about the Python-list
mailing list