Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Provide a 'key' argument for all() and any() builtins
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Chris Rebert  
View profile  
 More options Jul 23 2012, 12:39 am
From: Chris Rebert <pyid...@rebertia.com>
Date: Sun, 22 Jul 2012 21:39:13 -0700
Local: Mon, Jul 23 2012 12:39 am
Subject: Re: [Python-ideas] Provide a 'key' argument for all() and any() builtins

On Sun, Jul 22, 2012 at 9:03 PM, Giampaolo Rodolà <g.rod...@gmail.com> wrote:
> 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)

You're missing some brackets or parens there:
    all([a, b, c, ...], key=callable)

<snip>

> Thoughts?

I see negligible advantage over just writing the generator expression directly:
    all(callable(item) for item in iterable)
=== all(iterable, key=callable)
(which is about comparable to the difference between a map()
invocation vs. a list comprehension, and the choice there seems to be
largely a matter of taste)

min() & max() return the result object satisfying the constraint, so
the `key` argument makes sense for when you want to e.g. find the
cheapest house rather than the price of the cheapest house.
In contrast, any() & all() *always* just return a simple bool result,
not the object whose truthiness determined the predicate's result, so
`key` would be pointless since the result gets converted to a plain
bool anyway (or at least, we can conceptualize the implementation as
if it worked that way).

Cheers,
Chris
_______________________________________________
Python-ideas mailing list
Python-id...@python.org
http://mail.python.org/mailman/listinfo/python-ideas


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.