A patch to support L.count(value, cmp=None, key=None)

John Machin sjmachin at lexicon.net
Tue Jun 19 00:25:00 CEST 2007


On Jun 19, 5:17 am, "BJörn Lindqvist" <bjou... at gmail.com> wrote:
>
>   persons.count("olle", key = attergetter("name"))
>
> is longer and just barely more readable than
>
>   sum(1 for x in persons if x.name == "olle"))
>

The OP's proposal seems to have a very narrow focus, whereas the
generator approach can handle a much wider range of queries:

 sum(1 for x in persons if x.name == "olle" and x.country == "se"))
 sum(x.salary for x in persons if x.name == "olle"))

By the time one has looked up the syntax for the augmented count
method, remembered the existence of something like "attergetter" [sic]
and nutted out its spelling and usage, somebody else using generators
would have the job and gone to lunch :-)

YAGNI. Case dismissed.




More information about the Python-list mailing list