8000 gh-127750: Improve repr of functools.singledispatchmethod by serhiy-storchaka · Pull Request #130220 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-127750: Improve repr of functools.singledispatchmethod #130220

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

serhiy-storchaka
Copy link
Member
@serhiy-storchaka serhiy-storchaka commented Feb 17, 2025

@serhiy-storchaka
Copy link
Member Author

I am not sure whether "single dispatch" or even "single dispatch method" should be written as a single word.

@eendebakpt
Copy link
Contributor

I am not sure whether "single dispatch" or even "single dispatch method" should be written as a single word.

I would prefer "single dispatch method" in text. Writing it as "singledispatchmethod" would match the name python object in functools, which would not be bad either. So both are fine with me

This is what the output looks like now:

<single dispatch method A.dp>
<bound single dispatch method A.dp of <__main__.A object at 0x0000023B84D83A10>>
<function A.normal at 0x0000023B84F2A090>
<bound method A.normal of <__main__.A object at 0x0000023B84D83A10>>

With script

from functools import *

class A:
    
    @singledispatchmethod
    def dp(self, x):
        return x
    
    def normal(self, x):
        return x
    
a = A()

print(repr(A.dp))
print(repr(a.dp))

print(repr(A.normal))
print(repr(a.normal))

I would change the representation of the A.dp to include the id. And maybe change the text from method to function?

@rhettinger rhettinger removed their request for review February 18, 2025 17:56
@serhiy-storchaka
Copy link
Member Author

The difference between A.normal and A.dp is that A.normal is a long living object, while A.dp is a short-living temporary object which will usually disappear after the call, unless you keep a reference to it. And usually you do not have reason to keep a reference: if dp is a normal method, A.dp will not even work, unlike to the bound A().dp. It can only work if dp is a class or static method, but in that case it is easier to use a normal function with singledispatch.

For function, the id can be needed to distinguish closures created by the same code that have the same name.

So I do not think that there is a need to expose the id of the _singledispatchmethod_get object. The name should usually be enough.

@serhiy-storchaka serhiy-storchaka merged commit f33d21e into python:main Mar 5, 2025
41 checks passed
@serhiy-storchaka serhiy-storchaka deleted the singledispatchmethod-repr branch March 5, 2025 11:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0