We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
__add__
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
GitHub fields:
assignee = None closed_at = <Date 2021-08-05.00:08:48.071> created_at = <Date 2020-09-03.20:30:50.412> labels = ['3.7', '3.8', '3.9', '3.10', 'type-feature', 'docs'] title = 'docs: operator dunder (`__add__`, et al.) invocations described incorrectly' updated_at = <Date 2021-08-05.00:08:48.071> user = 'https://github.com/wchargin'
bugs.python.org fields:
activity = <Date 2021-08-05.00:08:48.071> actor = 'brett.cannon' assignee = 'docs@python' closed = True closed_date = <Date 2021-08-05.00:08:48.071> closer = 'brett.cannon' components = ['Documentation'] creation = <Date 2020-09-03.20:30:50.412> creator = 'wchargin' dependencies = [] files = [] hgrepos = [] issue_num = 41706 keywords = ['patch'] message_count = 2.0 messages = ['376316', '398954'] nosy_count = 3.0 nosy_names = ['brett.cannon', 'docs@python', 'wchargin'] pr_nums = ['22084'] priority = 'normal' resolution = 'fixed' stage = 'resolved' status = 'closed' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue41706' versions = ['Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10']
The text was updated successfully, but these errors were encountered:
The operator override dunder methods, like __add__, are described in the “Data model” docs here: https://docs.python.org/3.10/reference/datamodel.html#object.__add__
Those docs say:
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.
x + y
x
__add__()
x.__add__(y)
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.
type(x).__add__
x.__add__
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.
__radd__
I have a patch that I can submit as a PR; just need a bpo number.
Sorry, something went wrong.
New changeset 80f33f2 by William Chargin in branch 'main': bpo-41706: Fix special method invocation docs to mention using type() (GH-22084) 80f33f2
No branches or pull requests
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: