Hi Simon,

> On 2012-05-08, Florent Hivert <florent.hiv...@lri.fr> wrote:
> > But it is never called. I'd like to have confirmation that adding to all
> > vectors classes (following sage/structure/element.pyx line 881 (5.0.rc0))
> >
> >     def __richcmp__(left, right, int op):
> >         return (<Element>left)._richcmp(right, op)
> >
> > is the correct solution.
> 
> Yes, the following advices given in some comment in
> sage/structure/element.pyx are still valid (and, I agree, difficult
> to keep in mind):
> 
>     ####################################################################
>     # For a derived Cython class, you **must** put the following in
>     # your subclasses, in order for it to take advantage of the
>     # above generic comparison code.  You must also define
>     # either _cmp_c_impl (if your subclass is totally ordered),
>     # _richcmp_c_impl (if your subclass is partially ordered), or both
>     # (if your class has both a total order and a partial order;
>     # then the total order will be available with cmp(), and the partial
>     # order will be available with the relation operators; in this case
>     # you must also define __cmp__ in your subclass).
>     # This is simply how Python works.
>     #
>     # For a *Python* class just define __cmp__ as always.
>     # But note that when this gets called you can assume that
>     # both inputs have identical parents. 
>     #
>     # If your __cmp__ methods are not getting called, verify that the 
>     # canonical_coercion(x,y) is not throwing errors. 
>     #
>     ####################################################################
> 
> IIRC, the problem is that (by design of Cython) it is not possible to
> inherit __(rich)cmp__  if there is also a __hash__ method. Anyway,
> copying the code for __(rich)cmp__ from sage/structure/element.pyx is
> necessary, inheritance wouldn't work, as I've learnt the hard way...

And it's not the end of the story: you also need to overload __hash__ if you
do that. Cython assumes that if you change equality, you have to change
__hash__ as well. __hash__ isn't inherited.

Florent

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to