[Python-Dev] NotImplemented comparisons

Georg Brandl g.brandl at gmx.net
Thu Aug 2 23:25:27 CEST 2007


Terry Reedy schrieb:
> "Facundo Batista" <facundobatista at gmail.com> wrote in message 
> news:e04bdf310708021111g2870662bo5c6fdb3c1c68a9c2 at mail.gmail.com...
> | >>> class C(object):
> | ...     def __cmp__(self, other):
> | ...         return NotImplemented
> | ...
> 
> Given that you 'should' return an int, doing elsewise has undefined 
> results.

Returning anything other than an int or NotImplemented raises an exception.
NotImplemented seems to be special cased so that the other object's
__cmp__ can be tried too.

> | >>> c = C()
> | >>> print c < None
> 
> I presume that this translates into c.__compare(None) <  0 which becomes 
> NotImplemented < 0.  The result of that is undefined and interpreter 
> dependent.

No, it becomes id(c) < id(None). See half_compare in Objects/typeobject.c.


> This is still NotImplemented < 0 versus NotImplemented < None.  As I 
> understand, such nonsense comparisions will raise exceptions in 3.0.

Yes, fortunately.

Georg

-- 
Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.



More information about the Python-Dev mailing list