#18329: Inherit __richcmp__ and __cmp__ in subclasses of Element
-------------------------------------+-------------------------------------
Reporter: jdemeyer | Owner:
Type: enhancement | Status: needs_review
Priority: major | Milestone: sage-6.7
Component: coercion | Resolution:
Keywords: | Merged in:
Authors: Jeroen Demeyer | Reviewers:
Report Upstream: N/A | Work issues:
Branch: | Commit:
u/jdemeyer/ticket/18329 | 4b08519ef79528cce58738b501751f1d5db9f519
Dependencies: | Stopgaps:
-------------------------------------+-------------------------------------
Comment (by SimonKing):
Replying to [comment:28 jdemeyer]:
> Replying to [comment:27 SimonKing]:
> > Can you tell how it actually works?
> There are two things you need to know:
>
> 1. The Cython "metaclass" mechanism implemented in #18330. This is
similar to but not the same as a Python metaclass. It is a mechanism to
change the `type()` of a Cython extension type and call the `__init__`
method (but not `__new__`!) of the metaclass. Using the C preprocessor, a
hook to implement this is added which is called whenever a Cython
extension type is created.
OK, I should have a look at that.
> 2. This ticket uses that mechanism to manually inherit the `tp_compare`
and `tp_richcompare` slots of extension types, see the code in
`src/sage/misc/inherit_comparison.pyx` in the branch.
I still don't understand what it is supposed to mean. What do you mean by
"manually inherit"?
> > And of course, when cythoning the elements of a combinatorial free
module, `__cmp__` and `__richcmp__` would still be copied rather than
inherited from sage.structure.element.
> What do you mean with "would still be copied"? I don't understand what
you're trying to say.
See the comments in sage.structure.element:
{{{
####################################################################
# For a derived Cython class, you **must** put the __richcmp__
# method below in your subclasses, in order for it to take
# advantage of the above generic comparison code.
#
# You must also define either _cmp_ (if your subclass is totally
# ordered), _richcmp_ (if your subclass is partially ordered), or
# both (if your class has both a total order and a partial order,
# or if that gives better performance).
#
# Rich comparisons (like a < b) will default to using _richcmp_,
# three-way comparisons (like cmp(a,b)) will default to using
# _cmp_. But if you define just one of _richcmp_ and _cmp_, it will
# be used for all kinds of comparisons.
#
# In the _cmp_ and _richcmp_ methods, you can assume that both
# arguments have identical parents.
####################################################################
}}}
My understanding was that with the ticket here it would in future NOT be
needed to copy the `__cmp__` and `__richcmp__` methods.
--
Ticket URL: <http://trac.sagemath.org/ticket/18329#comment:29>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/d/optout.