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
Kirill Smelkov
cython
Commits
51b2f29d
Commit
51b2f29d
authored
May 26, 2021
by
Matti Picus
Committed by
GitHub
May 26, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
DOC: add documentation for total_ordering decorator (GH-4195)
parent
e36091ff
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
3 deletions
+22
-3
docs/src/userguide/special_methods.rst
docs/src/userguide/special_methods.rst
+22
-3
No files found.
docs/src/userguide/special_methods.rst
View file @
51b2f29d
...
@@ -165,14 +165,14 @@ which always take `self` as the first argument.
...
@@ -165,14 +165,14 @@ which always take `self` as the first argument.
Rich comparisons
Rich comparisons
-----------------
-----------------
There are
two
ways to implement comparison methods.
There are
a few
ways to implement comparison methods.
Depending on the application, one way or the other may be better:
Depending on the application, one way or the other may be better:
*
The first way uses
the 6 Python
*
Use
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>`_
:meth:`__eq__`, :meth:`__lt__`, etc.
:meth:`__eq__`, :meth:`__lt__`, etc.
This is new since Cython 0.27 and works exactly as in plain Python classes.
This is new since Cython 0.27 and works exactly as in plain Python classes.
*
The second way uses
a single special method :meth:`__richcmp__`.
*
Use
a single special method :meth:`__richcmp__`.
This implements all rich comparison operations in one method.
This implements all rich comparison operations in one method.
The signature is ``def __richcmp__(self, other, int op)``.
The signature is ``def __richcmp__(self, other, int op)``.
The integer argument ``op`` indicates which operation is to be performed
The integer argument ``op`` indicates which operation is to be performed
...
@@ -194,6 +194,25 @@ Depending on the application, one way or the other may be better:
...
@@ -194,6 +194,25 @@ Depending on the application, one way or the other may be better:
These constants can be cimported from the ``cpython.object`` module.
These constants can be cimported from the ``cpython.object`` module.
* Use a ``cython.total_ordering`` decorator, which is a re-implementation of
the `functools.total_ordering
<https://docs.python.org/3/library/functools.html#functools.total_ordering>`_
decorator. It can only be used on a ``cdef`` class:
.. code-block:: cython
@cython.total_ordering
cdef class ExtGe:
cdef int x
def __ge__(self, other):
if not isinstance(other, ExtGe):
return NotImplemented
return self.x >= (<ExtGe>other).x
def __eq__(self, other):
return isinstance(other, ExtGe) and self.x == (<ExtGe>other).x
.. _the__next__method:
.. _the__next__method:
The :meth:`__next__` method
The :meth:`__next__` method
...
...
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