Homunculus Reticulli
Homunculus Reticulli

Reputation: 68466

Inequality operators and sorting in Python

I have a collection (list) of objects. I want to be able to sort the objects without having to provide a callback (lambda function) to sorted() but instead, by implementing functions that could be used to ascertain strict (in)equality between objects of the same type - ala C++.

The __eq__() function implemented on a class allows equality checks. I can't seem to find any similar functions to implement < and >

How may I implement this in Python?

Upvotes: 0

Views: 238

Answers (2)

Mike Ramirez
Mike Ramirez

Reputation: 10970

http://docs.python.org/reference/datamodel.html#basic-customization

You're looking for

__lt__() and __gt__() 

(it's down just a tad under that link).

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799150

Via __lt__() and __gt__().

Upvotes: 11

Related Questions