[issue41706] docs: operator dunder (`__add__`, et al.) invocations described incorrectly
New submission from William Chargin
For instance, to evaluate the expression `x + y`, where `x` is an instance of a class that has an `__add__()` method, `x.__add__(y)` is called.
But this is not correct: `x + y` always uses `type(x).__add__`, which
may be different from `x.__add__` if `__add__` has been set directly on
the instance.
Demonstration:
```
class C:
def __add__(self, other):
return "from class"
c = C()
print(c + c) # prints "from class"
c.__add__ = lambda other: "from instance"
print(c.__add__(c)) # prints "from instance"
print(type(c).__add__(c, c)) # prints "from class"
print(c + c) # prints "from class"!
```
The same issue appears in the reversed operator dunder (`__radd__`,
et al.) docs below.
I have a patch that I can submit as a PR; just need a bpo number.
----------
assignee: docs@python
components: Documentation
messages: 376316
nosy: docs@python, wchargin
priority: normal
severity: normal
status: open
title: docs: operator dunder (`__add__`, et al.) invocations described incorrectly
type: enhancement
versions: Python 3.10, Python 3.6, Python 3.7, Python 3.8, Python 3.9
_______________________________________
Python tracker
Change by William Chargin
participants (1)
-
William Chargin