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

Hannu Kankaanp?? hanzspam at
Fri Nov 21 09:12:15 CET 2003

"Peter Milliken" <peterm at> wrote in message news:<cgavb.543$WD1.13154 at>...
> 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,

More information about the Python-list mailing list