8000 bpo-41706: Fix dunder operator invocation docs by wchargin · Pull Request #22084 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-41706: Fix dunder operator invocation docs #22084

New issue

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

Merged

Conversation

wchargin
Copy link
Contributor
@wchargin wchargin commented Sep 3, 2020

The expression x + y is evaluated using type(x).__add__, which may
differ from x.__add__ if the __add__ attribute has been directly
assigned 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"!

wchargin-branch: dunder-op-invocation-docs

https://bugs.python.org/issue41706

The expression `x + y` is evaluated using `type(x).__add__`, which may
differ from `x.__add__` if the `__add__` attribute has been directly
assigned on the instance.

Demonstration:

```python
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"!
```

wchargin-branch: dunder-op-invocation-docs
wchargin-source: 543786396ecba124a332357c771cfd6b20493abe
@wchargin
Copy link
Contributor Author
wchargin commented Sep 3, 2020

Verified that doc updates render correctly in the build output from the
GitHub Action.

@ammaraskar
Copy link
Member

@brettcannon Do you have an opinion on this tweak to the documentation since you wrote the desugarring blog posts?

@brettcannon
Copy link
Member

Thanks!

@wchargin wchargin deleted the wchargin-dunder-op-invocation-docs branch August 4, 2021 20:45
@wchargin
Copy link
Contributor Author
wchargin commented Aug 4, 2021

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0