Path: blob/master/sage/ext/notes/lenard_lindstrom-richcmp.txt
4108 views
Apparently an extension type subclass will only inherit its base1class rich comparison function if the subclass does not define a hash2function. This is a CPython thing that has nothing to do with Pyrex.3I had to look at the Python interpreter source code to discover what4was happening.56Two other things. First it is dangerous to cast an object pointer to7something else. No type checking is done by Pyrex so this could lead8to incorrect memory accesses. Second, if your type does not support a9particular kind of comparison then __richcmp__ should return10NotImplemented, a builtin object. This lets Python try other11comparisons. So your comparison function should look like something12like this:1314def __richcmp__(x, y, case):15print "__richcmp__"16cdef Base self, other17try:18self = x19other = y20if case == 2:21return bool(self.a == other.a)22except TypeError:23pass24return NotImplemented2526Of cource you might also want to implement != as well.2728Lenard Lindstrom29<[email protected]>3031