#18305: Element comparison for Python classes
-------------------------------------+-------------------------------------
Reporter: vdelecroix | Owner:
Type: enhancement | Status: needs_review
Priority: major | Milestone: sage-6.7
Component: coercion | Resolution:
Keywords: | Merged in:
Authors: Vincent Delecroix | Reviewers:
Report Upstream: N/A | Work issues:
Branch: | Commit:
u/vdelecroix/18305 | 81be759f0e28dc13aa17e2f7df72bee0e729ee8f
Dependencies: #17890 | Stopgaps:
-------------------------------------+-------------------------------------
Changes (by vdelecroix):
* status: new => needs_review
Old description:
> In #17890 the comparison of elements (i.e. `Element.__cmp__`) and rich
> comparison of elements (i.e. `Element.__richcmp__`) have been simplified.
>
> 1 .If coercion has to be involved then the Python elements should
> implement:
> - `_richcmp_(left, right, op)` for rich comparisons (i.e. operators `<`,
> `<=`, `==`, `!=`, `>=` and `>=`)
> - `_cmp_(left, right)` for comparisons (i.e. when calling `cmp(x,y)`)
> In these two methods, it can be assumed that both arguments `left` and
> `right` are subclasses of `Element` with the same parent. If `_cmp_`
> makes sense it is possible to implement it only. If an element wants to
> implement `==` and `!=` only, then it should just raise a
> `NotImplementedError` for other operators in `_richcmp_(left,right,op)`.
>
> 2 . If comparison needs to not avoid coercion, then:
> - rich comparison is possible through `__eq__`, `__ne__`, `__lt__`, etc
> (and all of these should be implemented)
> - comparison is possible via `__cmp__`
>
> With the current branch:
> - make everything needed for rich comparisons available from Python: the
> branch adds `py_rich_to_bool` and `py_rich_to_bool_sgn` and `op_EQ`,
> `op_NE`, `op_LT`, `op_LE`, `op_GE` and `op_GT` in
> `sage.structure.sage_object`
> - six elements are modified to use either `_richcmp_` or `_cmp_` instead
> of `__eq__`/`__ne__`/`__lt__`, etc (and #18303 takes care of
> `sage.rings.qqbar`)
>
> Still to do or discuss:
> - update the documentation in `sage.structure.Element`
> - decide what should we do with respect to `x < y` when this makes no
> sense (some elements do raise a `TypeError`)
> - remove the test in the default `Element._cmp_` since an element
> implementing `__cmp__` wants to avoid coercion (?)
New description:
In #17890 the comparison of elements (i.e. `Element.__cmp__`) and rich
comparison of elements (i.e. `Element.__richcmp__`) have been simplified.
1 .If coercion has to be involved then the Python elements should
implement:
- `_richcmp_(left, right, op)` for rich comparisons (i.e. operators `<`,
`<=`, `==`, `!=`, `>=` and `>=`)
- `_cmp_(left, right)` for comparisons (i.e. when calling `cmp(x,y)`)
In these two methods, it can be assumed that both arguments `left` and
`right` are subclasses of `Element` with the same parent. If `_cmp_` makes
sense it is possible to implement it only. If an element wants to
implement `==` and `!=` only, then it should just raise a
`NotImplementedError` for other operators in `_richcmp_(left,right,op)`.
2 . If comparison needs to not avoid coercion, then:
- rich comparison is possible through `__eq__`, `__ne__`, `__lt__`, etc
(and all of these should be implemented)
- comparison is possible via `__cmp__`
With the current branch:
- make everything needed for rich comparisons available from Python: the
branch adds `py_rich_to_bool` and `py_rich_to_bool_sgn` and `op_EQ`,
`op_NE`, `op_LT`, `op_LE`, `op_GE` and `op_GT` in
`sage.structure.sage_object`
- six elements are modified to use either `_richcmp_` or `_cmp_` instead
of `__eq__`/`__ne__`/`__lt__`, etc (and #18303 takes care of
`sage.rings.qqbar`)
Potentially to discuss:
- decide what should we do with respect to `x < y` when this makes no
sense (some elements do raise a `TypeError`)
- remove the test in the default `Element._cmp_` since an element
implementing `__cmp__` wants to avoid coercion
--
--
Ticket URL: <http://trac.sagemath.org/ticket/18305#comment:6>
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.