I am working with some existing code that overrides equality (using the __cmp__ method) for a class. It does not work as expected, and trying to fix it, I came across some kind of behavior that I do not understand. If you define __cmp__ for a class that just calls the cmp built-in function, I would expect it to always reach maximum recursion depth. However, if you try to compare an instance of a class with itself, it returns 0.
Here is the code:
class A: def __cmp__(self, other): return cmp(self, other) a = A() b = A() cmp(a, a)
RuntimeError I understand, but I do not understand why the first two cmp calls succeed.
I read the data model section in python docs and other things like this nice python partitioning equality , but cannot find the answer to this recursion.
And, yes, I understand that, as written, this is a completely meaningless class. The code I'm working with is trying to redefine equality in certain situations and otherwise gets into the base base. The base database does not work as it is implemented, and therefore I am trying to fix it. I thought the cmp call might work and found this problem. I hope that understanding this will help me find a suitable solution.
source share