[issue41911] Language reference incorrectly says comparison expressions return boolean values

Terry J. Reedy report at bugs.python.org
Fri Oct 2 21:43:58 EDT 2020


Terry J. Reedy <tjreedy at udel.edu> added the comment:

For rich comparisons, a proper explanation is given in the rich comparison entry in the datamodel section ( #41910) 

"A rich comparison method may return the singleton NotImplemented if it does not implement the operation for a given pair of arguments. By convention, False and True are returned for a successful comparison. However, these methods can return any value, so if the comparison operator is used in a Boolean context (e.g., in the condition of an if statement), Python will call bool() on the value to determine if the result is true or false."

bool(x) calls x.__bool__ and requires that the latter return False or True.

As for the other two comparisons, x is y always returns False or True for a proper implementation because int == int always does.  If y.__contains__(x) does not raise, x in y returns bool(y.__contains__(x)), clamping aberrant implementations of __contains__.

A replacement text might be 

"Comparisons normally yield boolean values: True or False.  But rich comparisons not in a Boolean context may yield anything."

Possibly link 'rich comparisons' to the datamodel section.

----------
nosy: +terry.reedy

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41911>
_______________________________________


More information about the Python-bugs-list mailing list