operator overloading
Ken Seehof
12klat at sightreader.com
Tue Jun 6 06:41:22 EDT 2000
Thomas Wouters wrote:
> 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.
I was being facetious and forgot to add a ":-)" to the "convention" comment. In
other words, I was trying to be nice (in a silly way) to the author of the previous
"solution" by not actually saying that the answer was completely wrong. Hence, in
the spirit of generosity, I referred to the boolean interpretation of 0 and 1, the
meaning of ">" and "<", and the return value of __cmp__ as "conventions".
> > ... 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)
Good point (that is how I would write it in an actual program). Of course, your
solution hides the semantics of the __cmp__ return value, which is fine, unless you
happen to be writing an example specifically to show the semantics of the __cmp__
return value. :-)
> 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!
- Ken Seehof
kens at sightreader.cHi! I'm a .signature virus! copy me into your .signature file to
help me spread!om
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 12klat.vcf
Type: text/x-vcard
Size: 343 bytes
Desc: Card for Ken Seehof
URL: <http://mail.python.org/pipermail/python-list/attachments/20000606/b9e8ae3b/attachment.vcf>
More information about the Python-list
mailing list