Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
b6648eb1
Commit
b6648eb1
authored
Dec 08, 2017
by
Jeroen Demeyer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Undeprecate __richcmp__
parent
e2a70313
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
35 deletions
+47
-35
docs/src/reference/extension_types.rst
docs/src/reference/extension_types.rst
+13
-8
docs/src/userguide/special_methods.rst
docs/src/userguide/special_methods.rst
+34
-27
No files found.
docs/src/reference/extension_types.rst
View file @
b6648eb1
...
@@ -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 |
...
...
docs/src/userguide/special_methods.rst
View file @
b6648eb1
...
@@ -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
^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment