a_list.count(a_callable) ?

Carsten Haese carsten at uniqsys.com
Fri Jun 15 20:39:55 CEST 2007


On Fri, 2007-06-15 at 17:55 +0000, Ping wrote:
> On 6 16 ,   12 33 , Carsten Haese <cars... at uniqsys.com> wrote:
> > Did you see my alternative example on this thread? It allows you to use
> > list.count in almost exactly that way, except that instead of passing
> > the callable directly, you pass an object that defers to your callable
> > in its __eq__ method.
> >
> > HTH,
> >
> > --
> > Carsten Haesehttp://informixdb.sourceforge.net
> 
> Yes, I read it.  This works, but it may make lots of dummy classes
> with the special __eq__ method if I have many criteria to use for
> counting.

No, it won't. You only need one class that is given the criterion at
instantiation time. Something like this:

class WhereTrue(object):
    def __init__(self, func):
        self.func = func
    def __eq__(self, other):
        return self.func(other)

list1.count(WhereTrue(callable1))
list2.count(WhereTrue(callable2))

HTH,

-- 
Carsten Haese
http://informixdb.sourceforge.net





More information about the Python-list mailing list