[Python-Dev] NotImplemented comparisons

Terry Reedy tjreedy at udel.edu
Thu Aug 2 22:11:35 CEST 2007


"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.

| >>> 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.

| >>> print NotImplemented < None

As is this.  There is no reason to expect the two comparisons 
(NotImplemented to 0 and None) to give the same or different results.

| Does somebody know why is a difference here?

Different interpreters, different arbitrary results.  I believe checking 
the ids of the right objects (the type objects, I have read) would explain.

| Furthermore, we can check that is a problem regarding __cmp__:
|
| >>> class C(object):
|    def __cmp__(self, other):
|        return NotImplemented
|    def m(self):
| return NotImplemented
|
| >>> c = C()
| >>> print c < None
| True
| >>> print c.m() < None
| False

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

tjr





More information about the Python-Dev mailing list