I have the following code:
a = str('5')
b = int(5)
a == b
# False
But if I make a subclass of int
, and reimplement __cmp__
:
class A(int):
def __cmp__(self, other):
return super(A, self).__cmp__(other)
a = str('5')
b = A(5)
a == b
# TypeError: A.__cmp__(x,y) requires y to be a 'A', not a 'str'
Why are these two different? Is the python runtime catching the TypeError thrown by int.__cmp__()
, and interpreting that as a False
value? Can someone point me to the bit in the 2.x cpython source that shows how this is working?