operator overloading

Ken Seehof 12klat at sightreader.com
Tue Jun 6 18:49:53 EDT 2000


Comparisons are evaluated from left to right:
   "a>b>c" is the same as "(a>b) and (b>c)"

Operator functions are searched from left to right.
  "a>b" looks for a.__cmp__
   If not found, (or a type exception is thrown), it looks for b.__cmp__.

>>> class A:
...  def __cmp__(self,b):
...   print 'A.__cmp__'
...   return 0
...
>>> class B:
...  def __cmp__(self,b):
...   print 'B.__cmp__'
...   return 0
...
>>> a = A()
>>> b = B()
>>> a>b
A.__cmp__
0
>>> b<a
B.__cmp__
0
>>> a < b < "vorpal bunny"
A.__cmp__
0
>>> a < "vorpal bunny" < b
A.__cmp__
0
>>> "vorpal bunny" < b < a
B.__cmp__
0

--
Ken Seehof
kens at sightreader.com
starship.python.net/crew/seehof
Hi! I'm a .signature virus! copy me into your .signature file to help me spread!

François Pinard wrote:

> david_ullrich at my-deja.com écrit:
>
> > > If one wants tools like `sort()' to work properly, `__cmp__' should
> > > establish a proper order relation, yes.  Otherwise, rather cryptic
> > > bugs might pop out.
>
> > If so this should probably be mentioned in the docs.
>
> _If_ the documentation does not make it clear, it should indeed.  Could
> someone check and, if the documentation is improper, report it appropriately?
>
> --
> François Pinard   http://www.iro.umontreal.ca/~pinard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20000606/d3a1abcd/attachment.html>
-------------- 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/d3a1abcd/attachment.vcf>


More information about the Python-list mailing list