How do you pass a standard operator such as '<' as a parameter?

Hannu Kankaanp?? hanzspam at yahoo.com.au
Fri Nov 21 03:12:15 EST 2003


"Peter Milliken" <peterm at resmed.com.au> wrote in message news:<cgavb.543$WD1.13154 at nnrp1.ozemail.com.au>...
> Wrote too soon - the obvious answer is not to use a "comparison" function at
> all when instantiating the class but rather to just code the "<" in the
> class "add" procedure and then make sure the data type that is being used
> contains a __lt__ operator :-) i.e. the class definition should just be:

Huh? I was certain you wanted to use comparison function to get
flexibility, but by hard-wiring < to the code you'll lose that.
One might want to have the list ordered by some other property,
e.g. from largest element to smallest, or by tuple's second element.
Someone might want to put the same objects in several containers, and
the elements would be sorted differently in each container.

You could just add "less than" as a default behaviour
def __init__ (self, comparator=operator.lt):




More information about the Python-list mailing list