[Python-Dev] Small any/all enhancement
"Martin v. Löwis"
martin at v.loewis.de
Tue Dec 27 22:49:58 CET 2005
Valentino Volonghi aka Dialtone wrote:
> What I would like to ask, before it's too late, is if it's possible to add an
> optional test argument.
I don't see why: you can easily synthesize that with map/imap.
> These would be the new proposed APIs. The usecase is to be able to extract
> attributes from objects or simply to have arbitrary checks like:
>
> import operator
>
> any(some_objects, test=operator.attrgetter('some_attribute'))
So write
any(map(operator.attrgetter('some_attribute'), some_objects))
# same number of characters to type
any(o.some_attribute for o in some_objects)
# fewer number of characters
> def zerop(x):
> return x==0
>
> all(some_objects, zerop)
So write
all(map(some_objects, zerop))
or
all(o==0 for o in some_objects)
# avoids defining zerop
> instead of preprocessing the generator with a generator expression before
> passing it to any/all.
What is the disadvantage of such "preprocessing"?
Regards,
Martin
More information about the Python-Dev
mailing list