operator overloading
Thomas Wouters
thomas at xs4all.net
Mon Jun 5 17:50:10 EDT 2000
On Mon, Jun 05, 2000 at 03:02:47PM -0700, Ken Seehof wrote:
> The following more closely conforms to these conventions:
The return code of __cmp__ is hardly a convention! If you dont return one of
those values, in a consistent manner, you're going to confuse the users of
your class greatly. And possibly break things like list.sort()'s default
sort function.
> ... def __cmp__(self,other):
> ... if self.a == other.a:
> ... return 0
> ... elif self.a < other.a:
> ... return -1
> ... else:
> ... return 1
Good example not withstanding, you can of course write this much shorter,
and more obvious, and less errorprone:
def __cmp__(self, other):
return cmp(self.a, other.a)
This will even work if __cmp__ would be changed to return different values
;)
--
Thomas Wouters <thomas at xs4all.net>
Hi! I'm a .signature virus! copy me into your .signature file to help me spread!
More information about the Python-list
mailing list