REPOST: Re: cmp [__eq__]

Daniel Yoo dyoo at hkn.eecs.berkeley.edu
Thu Dec 27 15:42:31 EST 2001


Mark McEahern <markjunk at mceahern.com> wrote:
:> Perhaps then could someone explain to me how one compares classes
:> useing the == operator, or indeed if this bad programming practice.

: By overloading the __cmp__ method.  Don't confuse the return value of
: __cmp__ with whether or not the result of the comparison is that the two
: instances being compared are equal.  Examine this sample and note that
: __cmp__ isn't merely used to determine equality.  It is also used to
: determine sort order.

And if you just want to be able to compare two things for equality,
it's probably better to write an __eq__ method instead.


Unlike __cmp__, __eq__ is meant to be a boolean function, so it should
be more familiar to use.  There's more information on __eq__ and the
other rich comparison methods here:

    http://www.python.org/doc/current/ref/customization.html

Good luck!

========= WAS CANCELLED BY =======:
Path: news.sol.net!spool1-milwwi.newsops.execpc.com!newsfeeds.sol.net!newspump.sol.net!nntp1.roc.gblx.net!nntp.gblx.net!nntp1.phx1.gblx.net!nntp.gblx.net!nntp.gblx.net!enews.sgi.com!news-xfer.nuri.net!news-hub.kaist.ac.kr!feeder.kornet.net!news1.kornet.net!ua4canc3ll3r
From: Daniel Yoo <dyoo at hkn.eecs.berkeley.edu>
Newsgroups: comp.lang.python
Subject: cmsg cancel <a0g13n$23u5$1 at agate.berkeley.edu>
Control: cancel <a0g13n$23u5$1 at agate.berkeley.edu>
Date: Mon, 31 Dec 2001 04:36:47 GMT
Organization: A poorly-installed InterNetNews site
Lines: 2
Message-ID: <cancel.a0g13n$23u5$1 at agate.berkeley.edu>
NNTP-Posting-Host: 211.57.49.2
X-Trace: news2.kornet.net 1009775587 27193 211.57.49.2 (31 Dec 2001 05:13:07 GMT)
X-Complaints-To: usenet at news2.kornet.net
NNTP-Posting-Date: Mon, 31 Dec 2001 05:13:07 +0000 (UTC)
X-No-Archive: yes
X-Unac4ncel: yes
X-Commentary: I love NewsAgent 1.10 and the Sandblaster Cancel Engine Build 74 (19 March 1999)

This message was cancelled from within Mozilla.



More information about the Python-list mailing list