1: There are cases where one may need the __name__/__qualname__ of a given callable. If someone uses partial to create a new callable, there is no __name__/__qualname__ given. In my particular case, I'm logging what callback function is passed to a different function... if someone uses partial, there is no __name__/__qualname__ which leads to a current traceback... of course i can work around it but still was an odd case to me.

Per the docs on functools.partial: 
"Return a new partial object which when called will behave like func called with the positional arguments args and keyword arguments keywords"
... which made me initially think that in order to behave like the passed in function: it should have __name__ and __qualname__... like the func did.

2: I would say have both __qualname__ and __name__. Both could be based off of __name__/__qualname__ of the passed in func.

3: This would be more difficult since you would have to disassemble the lambda to figure out the called method (or methods)... We can table the lambda discussion for the purpose of this idea. I recall that typically it is preferred to use partial over lambdas, so this could be an additional functionality/benefit of using partial over lambda.

Notes:
... __name__ being something like partial(foo, "x") would be fine with me... I just feel as though something should be there.

- Charlie Scott Machalow


On Mon, Aug 29, 2022 at 9:56 PM Paul Bryan <pbryan@anode.ca> wrote:
+0

Questions:

1. What's the use case for partial having __name__?
2. Does this imply it should have __qualname__ as well?
3. What name would be given to (an inherently anonymous) lambda?

Notes:

1. I would prefer __name__ to be more qualifying like its repr (e.g. partial(foo, "x") → "<partial foo>")


On Mon, 2022-08-29 at 21:31 -0700, Charles Machalow wrote:
Hey folks,

I propose adding __name__ to functools.partial.

>>> get_name = functools.partial(input, "name: ")
>>> get_name()
name: hi
'hi'
>>> get_name.__name__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'functools.partial' object has no attribute '__name__'
>>> get_name.func
<built-in function input>
>>> get_name.func.__name__
'input'


We could set __name__ based off of partial.func.__name__ or we could try to set it to something like 'partial calling func.__name__'

If the callable doesn't have a name, we could fall back to a None __name__ or set it to something generic. 

Even lambdas have __name__ set:

>>> l = lambda: input('name: ')
>>> l.__name__
'<lambda>'


This proposal makes __name__ on partial objects more useful than the current behavior of __name__ on lambda objects as well. We could port over similar functionality to lambda if we'd like.

- Charlie Scott Machalow
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/WK3FO357ORPVAD3XRUBRH6IHIYSPS3G2/
Code of Conduct: http://python.org/psf/codeofconduct/