[Python-Dev] Efficient predicates for the standard library

Gareth McCaughan gmccaughan at synaptics-uk.com
Mon Oct 6 07:19:54 EDT 2003


Chris Stork wrote:

> Now that there's a useful default meaning for pred, we should give
> it a default and make it an optional argument.  For this the order of
> arguments must be reversed.  This is different from itertools consistent
> use of iterables as last arguments.  I don't know if this is relevant
> here.  Anyway, since predicates are in general more useful like this
> I think it's the better choice.

Perhaps that's true for a piece of code given as an example.
I don't think it would be sensible if, as you propose, these
functions were to be put in the standard library, because
there's something better to do with the default args for
real applications.

    def any(pred, *iterables):

I think the ability to work with multiple sequences (and
not to have to use the argument order iter1, pred, iter2, ...)
is more important than the ability to avoid typing "bool,".

Another option would be

    def any(*iterables, pred=bool):
        for items in imap(None, *iterables):
            if pred(*items): return True
        return False

which looks to me like it offers the best of both worlds.

-- 
g





More information about the Python-Dev mailing list