Tiago Katcipis | 17 May 20:16
Picon

Best way to define comparison

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? and if there is no better what are the advantages and disvantages of them. Some articles talk about using cmp, and others about the eq...its a little confusing :-(


_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
bob gailer | 17 May 21:30
Picon

Re: Best way to define comparison

Tiago Katcipis 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? and if 
> there is no better what are the advantages and disvantages of them. 
> Some articles talk about using cmp, and others about the eq...its a 
> little confusing :-(

AFAIK either approach is OK. No advantages. (Other than writing one 
method vs 2).

--

-- 
Bob Gailer
919-636-4239 Chapel Hill, NC

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Kent Johnson | 18 May 00:40
Gravatar

Re: Best way to define comparison

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
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor


Gmane