Add PEP 207 guidance on rich comparisons to PEP 8.

This commit is contained in:
Raymond Hettinger 2011-04-06 13:53:31 -07:00
parent cfb1d26c38
commit bba94bd03d
1 changed files with 15 additions and 0 deletions

View File

@ -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<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