[Tutor] Best way to define comparison

Kent Johnson kent37 at tds.net
Sun May 18 00:40:22 CEST 2008


On Sat, May 17, 2008 at 2:18 PM, Tiago Katcipis <katcipis at inf.ufsc.br> wrote:
> Im writing a class on python and i want to implement the == and != operators
> on it. I have read about __cmp__ and about __eq__ for == and __ne__ for ! =.
> My question is... who is the better to use?

Use __cmp__() if it meets your needs - you just have to define one
method and you get all comparisons. The other special methods - "rich
comparisons" - were introduced in Python 2.1 to allow additional
flexibility when __cmp__() is not adequate.
http://www.amk.ca/python/2.1/index.html#SECTION000500000000000000000

Kent


More information about the Tutor mailing list