[Python-checkins] peps: Add PEP 207 guidance on rich comparisons to PEP 8.

raymond.hettinger python-checkins at python.org
Wed Apr 6 22:55:42 CEST 2011


http://hg.python.org/peps/rev/64bda015861d
changeset:   3862:64bda015861d
user:        Raymond Hettinger <python at rcn.com>
date:        Wed Apr 06 13:53:31 2011 -0700
summary:
  Add PEP 207 guidance on rich comparisons to PEP 8.

files:
  pep-0008.txt |  15 +++++++++++++++
  1 files changed, 15 insertions(+), 0 deletions(-)


diff --git a/pep-0008.txt b/pep-0008.txt
--- a/pep-0008.txt
+++ b/pep-0008.txt
@@ -667,6 +667,21 @@
       None was set to some other value.  The other value might have a type
       (such as a container) that could be false in a boolean context!
 
+    - When implementing ordering operations with rich comparisons, it is best to
+      implement all six operations (__eq__, __ne__, __lt__, __le__, __gt__,
+      __ge__) rather than relying on other code to only exercise a particular
+      comparison.
+
+      To minimize the effort involved, the functools.total_ordering() decorator
+      provides a tool to generate missing comparison methods.
+
+      PEP 207 indicates that reflexivity rules *are* assumed by Python.  Thus,
+      the interpreter may swap y>x with x<y, y>=x with x<=y, and may swap the
+      arguments of x==y and x!=y.  The sort() and min() operations are
+      guaranteed to use the < operator and the max() function uses the >
+      operator.  However, it is best to implement all six operations so that
+      confusion doesn't arise in other contexts.
+
     - Use class-based exceptions.
 
       String exceptions in new code are forbidden, because this language

-- 
Repository URL: http://hg.python.org/peps


More information about the Python-checkins mailing list