Add PEP 207 guidance on rich comparisons to PEP 8.
This commit is contained in:
parent
cfb1d26c38
commit
bba94bd03d
15
pep-0008.txt
15
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<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
|
||||
|
|
Loading…
Reference in New Issue