[Python-Dev] Fwd: summing a bunch of numbers (or "whatevers")

Tim Peters tim.one@comcast.net
Tue, 22 Apr 2003 23:59:27 -0400


[Greg Ward]
> Hmmm, a random idea: has filter() ever been used for anything else?
> I didn't think so.  So why not remove everything *except* that handy
> special-case: ie. in 3.0, filter(seq) == filter(None, seq) today, and
> that's *all* filter() does.
>
> Just a random thought...

It's been used for lots of other stuff, but I'm not sure if any other use
wouldn't read better as a listcomp.  For example, from spambayes:

def textparts(msg):
    """Return a set of all msg parts with content maintype 'text'."""
    return Set(filter(lambda part: part.get_content_maintype() == 'text',
                      msg.walk()))

I think that reads better as:

    return Set([part for part in msg.walk()
                     if part.get_content_maintype() == 'text'])

In Python 3.0 that will become a set comprehension <wink>:

    return {part for part in msg.walk()
                 if part.get_content_maintype() == 'text'}