from sage.structure.richcmp import richcmp_by_eq_and_lt,richcmp_method
@richcmp_method class A(object): def __init__(self,x): self._x = x __richcmp__ = richcmp_by_eq_and_lt("_eq","_lt") def _eq(self,other): if type(other)!=type(self): #other knows how to do the comparison return other.__eq__(self) return self._x == other._x def _lt(self,other): return self._x<other._x def __repr__(self): return "A %r" %self._x @richcmp_method class B(A): #I can do comparison myself def __init__(self,x,extra): A.__init__(self,x) __richcmp__ = richcmp_by_eq_and_lt("_eq","_lt") def _eq(self,other): print("hello") return self._x == other._x def _lt(self,other): return self._x<other._x def __repr__(self): return "B %r" %self._x There you go: sage: A(1) == B(1,"a") --------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-4-f5a54e86b883> in <module>() ----> 1 A(Integer(1)) == B(Integer(1),"a") /home/simon/sage/src/sage/structure/richcmp.pyx in sage.structure.richcmp.slot_tp_richcompare (build/cythonized/sage/structure/richcmp.c:1459)() 110 Function to put in the ``tp_richcompare`` slot. 111 """ --> 112 return self.__richcmp__(other, op) 113 114 /home/simon/sage/src/sage/structure/richcmp.pyx in sage.structure.richcmp.richcmp_by_eq_and_lt.richcmp (build/cythonized/sage/structure/richcmp.c:2178)() 317 if equal_types: 318 other_eq = getattr(other, eq_attr) --> 319 if other_eq(self): 320 return rich_to_bool(op, 0) 321 if op == Py_EQ: /home/simon/sage/src/sage/modules/bug.py in _eq(self, other) 10 if type(other)!=type(self): 11 #other knows how to do the comparison ---> 12 return other.__eq__(self) 13 return self._x == other._x 14 def _lt(self,other): ... last 3 frames repeated, from the frame below ... /home/simon/sage/src/sage/structure/richcmp.pyx in sage.structure.richcmp.slot_tp_richcompare (build/cythonized/sage/structure/richcmp.c:1459)() 110 Function to put in the ``tp_richcompare`` slot. 111 """ --> 112 return self.__richcmp__(other, op) 113 114 RuntimeError: maximum recursion depth exceeded while calling a Python object Enter code here... This is unexpected but we know what to do from sage.structure.richcmp import richcmp_by_eq_and_lt,richcmp_method @richcmp_method class A(object): def __init__(self,x): self._x = x __richcmp__ = richcmp_by_eq_and_lt("_eq","_lt") def _eq(self,other): if type(other)!=type(self): #other knows how to do the comparison return self.__eq__(other) return self._x == other._x def _lt(self,other): return self._x<other._x def __repr__(self): return "A %r" %self._x @richcmp_method class B(A): #I can do comparison myself def __init__(self,x,extra): A.__init__(self,x) __richcmp__ = richcmp_by_eq_and_lt("_eq","_lt") def _eq(self,other): print("hello") return self._x == other._x def _lt(self,other): return self._x<other._x def __repr__(self): return "B %r" %self._x ### reloading attached file bug.py modified at 12:00:42 ### sage: A(1) == B(1,"a") hello True -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+unsubscr...@googlegroups.com. To post to this group, send email to sage-devel@googlegroups.com. Visit this group at https://groups.google.com/group/sage-devel. For more options, visit https://groups.google.com/d/optout.