[Python-Dev] Small any/all enhancement (original) (raw)
"Martin v. Löwis" martin at v.loewis.de
Tue Dec 27 22:49:58 CET 2005
- Previous message: [Python-Dev] Small any/all enhancement
- Next message: [Python-Dev] Small any/all enhancement
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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(someobjects, test=operator.attrgetter('someattribute'))
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(someobjects, 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
- Previous message: [Python-Dev] Small any/all enhancement
- Next message: [Python-Dev] Small any/all enhancement
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]