[docs] [issue12067] Doc: remove errors about mixed-type comparisons.

Terry J. Reedy report at bugs.python.org
Sat Sep 22 02:05:24 CEST 2012

Terry J. Reedy added the comment:

After further thought: This section is about the syntax operators, not the special methods. The syntax operators never evaluate to NotImplemented as they (apparently) interpret its return from a special method the same as a raising of TypeError, and always raise TypeError when neither the op or its reflection is supported. So there should be no mention of NotImplemented here. Just a reference to 3.3. #15997 is related to my 'wonder' but not directly relevant to a patch for this. Please submit a draft patch when you have one.

I determined that 'raise TypeError' and 'return NotImplemented' both result in the call of the reflected method, at least for a couple of cases. (And same seems true for arithmetic ops too.)

class C():
    def __ge__(self, other):
        # print("in C.__ge__", end='')
        return True
    def __add__(self, other):
        return 44
    __radd__ = __add__

class O():
    def __le__(self, other):
        # print ("in O.__le__")
        return NotImplemented
    def __add__(self, other):
        return NotImplemented
c = C()
o = O()
ob = object() 
print(c >= o, o <= c, ob <= c)
# True True True
# print(ob <= ob) # raises TypeError
print(c + o, o + c, ob + c)
# 44 44 44
# print(ob + ob)  # raises TypeError
# print(ob >= o)  # with O.__le__ print uncommented
# in O.__le__  # so non-implemented reflected o <= ob *is* called
# TypeError: unorderable types: object() >= O()


Python tracker <report at bugs.python.org>

More information about the docs mailing list