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

2020-10-02 Thread Terry J. Reedy


Terry J. Reedy  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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2020-10-02 Thread Brett Cannon


New submission from Brett Cannon :

https://docs.python.org/3/reference/expressions.html#comparisons claims that 
"Comparisons yield boolean values: True or False." But that's not necessarily 
true:

```python
>>> class Spam: 
...  def __eq__(self, _): return 42
... 
>>> Spam() == object()
42
```

That really only happens when an expressions is used in a boolean context like 
an `if` statements guard expression.

--
assignee: brett.cannon
components: Documentation
messages: 377817
nosy: brett.cannon
priority: low
severity: normal
status: open
title: Language reference incorrectly says comparison expressions return 
boolean values
versions: Python 3.10, Python 3.8, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com