Right now: only implement __cmp__ following what is written in sage/structure/element.pyx, and not __eq__ and friends. You can play some tricks with the __richcmp__ hook but its probably best to stick to a single kind of comparison if you want your stuff to transition easily to python3.
On Sunday, October 26, 2014 9:57:49 AM UTC, Eric Gourgoulhon wrote: > > Hi, > > When implementing some algebra element class, I faced the following issue: > the class inherits from sage.structure.element.CommutativeAlgebraElement > and I've implemented the __eq__() and __ne__() methods, but not __cmp__() > because my algebra is not an ordered set. Everything works well (the test > suite for commutative algebras is passed by the parent), but if I construct > matrices from algebra elements f1,...,f4, via > a = matrix([[f1,f2], [f3,f4]]) > and compare them by a == b, the following error appears: > > NotImplementedError: BUG: sort algorithm for elements of 'algebra of > scalar fields on the 2-dimensional manifold 'M'' not implemented > > This is triggered by the lack of method __cmp__() in the algebra element > class. Adding such method via > > def __cmp__(self, other): > if self.__eq__(other): > return 0 > else: > return -1 > > fixes the problem. > > Hence the question: shall __cmp__() be always implemented in element > classes, even if the parent is not an ordered set ? If yes, what shall it > return when the two elements differ ? -1 as above ? Shall __eq__() be > implemented as well ? > I've noticed that in line 919 of file src/sage/structure/element.pyx, it > is written; > > # For a *Python* class just define __cmp__ as always. > > I've also noticed that in the example provided in the tutorial How to > implement new algebraic structures in Sage > <http://sagemath.org/doc/thematic_tutorials/coercion_and_categories.html>, > only __cmp__() is implemented, not __eq__(). > On the other side, __cmp__() has been withdrawn in Python 3 and only the > rich comparison methods, like __eq__(), remain. > > Thanks for your advice on this. > > Eric. > > -- 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 http://groups.google.com/group/sage-devel. For more options, visit https://groups.google.com/d/optout.