From bba94bd03df2a6b83bddf18ff0969fd80f2757a5 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 6 Apr 2011 13:53:31 -0700 Subject: [PATCH] Add PEP 207 guidance on rich comparisons to PEP 8. --- pep-0008.txt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pep-0008.txt b/pep-0008.txt index 6fa45db8e..652baf07e 100644 --- a/pep-0008.txt +++ b/pep-0008.txt @@ -667,6 +667,21 @@ Programming Recommendations 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=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