Commit b6648eb1 authored by Jeroen Demeyer's avatar Jeroen Demeyer

Undeprecate __richcmp__

parent e2a70313
...@@ -276,15 +276,20 @@ Arithmetic Methods ...@@ -276,15 +276,20 @@ Arithmetic Methods
Rich Comparisons Rich Comparisons
================ ================
* Starting with Cython 0.27, the Python There are two ways to implement comparison methods.
Depending on the application, one way or the other may be better:
* The first way uses the 6 Python
`special methods <https://docs.python.org/3/reference/datamodel.html#basic-customization>`_ `special methods <https://docs.python.org/3/reference/datamodel.html#basic-customization>`_
``__eq__``, ``__lt__``, etc. can be implemented. In previous versions, ``__richcmp__`` was ``__eq__``, ``__lt__``, etc.
the only way to implement rich comparisons, but is now deprecated. This is new since Cython 0.27 and works exactly as in plain Python classes.
* A single special method called ``__richcmp__()`` can be used to implement all the individual * The second way uses a single special method ``__richcmp__``.
rich compare, special method types. Note, however, that it is often easier to correctly This implements all rich comparison operations in one method.
implement the separate Python special methods than to get the ``__richcmp__()`` method right. The signature is ``def __richcmp__(self, other, int op)`` matching the
* ``__richcmp__()`` takes an integer argument, indicating which operation is to be performed `PyObject_RichCompare() <https://docs.python.org/3/c-api/object.html#c.PyObject_RichCompare>`_
as shown in the table below. Python/C API function.
The integer argument ``op`` indicates which operation is to be performed
as shown in the table below:
+-----+-----+-------+ +-----+-----+-------+
| < | 0 | Py_LT | | < | 0 | Py_LT |
......
...@@ -127,28 +127,34 @@ take `self` as the first argument. ...@@ -127,28 +127,34 @@ take `self` as the first argument.
Rich comparisons Rich comparisons
----------------- -----------------
Starting with Cython 0.27, the Python There are two ways to implement comparison methods.
`special methods <https://docs.python.org/3/reference/datamodel.html#basic-customization>`_ Depending on the application, one way or the other may be better:
:meth:``__eq__``, :meth:``__lt__``, etc. can be implemented. In previous versions,
:meth:``__richcmp__`` was the only way to implement rich comparisons. It takes an integer * The first way uses the 6 Python
indicating which operation is to be performed, as follows: `special methods <https://docs.python.org/3/reference/datamodel.html#basic-customization>`_
:meth:`__eq__`, :meth:`__lt__`, etc.
+-----+-----+-------+ This is new since Cython 0.27 and works exactly as in plain Python classes.
| < | 0 | Py_LT | * The second way uses a single special method :meth:`__richcmp__`.
+-----+-----+-------+ This implements all rich comparison operations in one method.
| == | 2 | Py_EQ | The signature is ``def __richcmp__(self, other, int op)``.
+-----+-----+-------+ The integer argument ``op`` indicates which operation is to be performed
| > | 4 | Py_GT | as shown in the table below:
+-----+-----+-------+
| <= | 1 | Py_LE | +-----+-------+
+-----+-----+-------+ | < | Py_LT |
| != | 3 | Py_NE | +-----+-------+
+-----+-----+-------+ | == | Py_EQ |
| >= | 5 | Py_GE | +-----+-------+
+-----+-----+-------+ | > | Py_GT |
+-----+-------+
The named constants can be cimported from the ``cpython.object`` module. | <= | Py_LE |
They should generally be preferred over plain integers to improve readabilty. +-----+-------+
| != | Py_NE |
+-----+-------+
| >= | Py_GE |
+-----+-------+
These constants can be cimported from the ``cpython.object`` module.
The :meth:`__next__` method The :meth:`__next__` method
---------------------------- ----------------------------
...@@ -209,6 +215,10 @@ Rich comparison operators ...@@ -209,6 +215,10 @@ Rich comparison operators
https://docs.python.org/3/reference/datamodel.html#basic-customization https://docs.python.org/3/reference/datamodel.html#basic-customization
You can choose to either implement the standard Python special methods
like :meth:`__eq__` or the single special method :meth:`__richcmp__`.
Depending on the application, one way or the other may be better.
+-----------------------+---------------------------------------+-------------+--------------------------------------------------------+ +-----------------------+---------------------------------------+-------------+--------------------------------------------------------+
| __eq__ |self, y | object | self == y | | __eq__ |self, y | object | self == y |
+-----------------------+---------------------------------------+-------------+--------------------------------------------------------+ +-----------------------+---------------------------------------+-------------+--------------------------------------------------------+
...@@ -222,13 +232,10 @@ https://docs.python.org/3/reference/datamodel.html#basic-customization ...@@ -222,13 +232,10 @@ https://docs.python.org/3/reference/datamodel.html#basic-customization
+-----------------------+---------------------------------------+-------------+--------------------------------------------------------+ +-----------------------+---------------------------------------+-------------+--------------------------------------------------------+
| __ge__ |self, y | object | self >= y | | __ge__ |self, y | object | self >= y |
+-----------------------+---------------------------------------+-------------+--------------------------------------------------------+ +-----------------------+---------------------------------------+-------------+--------------------------------------------------------+
| __richcmp__ |x, y, int op | object | Joined rich comparison method for all of the above | | __richcmp__ |self, y, int op | object | Joined rich comparison method for all of the above |
| | | | (deprecated, no direct Python equivalent) | | | | | (no direct Python equivalent) |
+-----------------------+---------------------------------------+-------------+--------------------------------------------------------+ +-----------------------+---------------------------------------+-------------+--------------------------------------------------------+
New code should better implement the separate Python special methods instead of trying to
correctly implement the joined ``__richcmp__()`` method.
Arithmetic operators Arithmetic operators
^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment