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
GH-100242: bring functools.py partial implementation more in line with C code #100244
base: main
Are you sure you want to change the base?
Conversation
in partial.__new__, before checking for the existence of the attribute 'func', first check whether the argument is an instance of partial.
|
Name | Link |
---|---|
97b9966 | |
https://app.netlify.com/sites/python-cpython-preview/deploys/6399fa45bbd72a0008fec2cf |
Lib/functools.py
Outdated
@@ -284,7 +284,7 @@ def __new__(cls, func, /, *args, **keywords): | |||
if not callable(func): | |||
raise TypeError("the first argument must be callable") | |||
|
|||
if hasattr(func, "func"): | |||
if isinstance(func, partial) and hasattr(func, "func"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will an instance of partial
ever not have a func
attribute? Could this be simplified to just an isinstance()
check?
if isinstance(func, partial) and hasattr(func, "func"): | |
if isinstance(func, partial): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that works for me too. (you can certainly remove the func
attribute from a pure python partial
instance, but the resulting object is not very useful)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we do that, maybe it's also a good idea to just use an isinstance check in the C implementation as well? then the two approaches are straightforwardly identical.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can certainly remove the func attribute from a pure python partial instance, but the resulting object is not very useful
Yeah, if people are deleting the func
attribute from partial
objects, I have a lot of questions about their broader approach to programming in general :)
maybe it's also a good idea to just use an isinstance check in the C implementation as well?
I'm not qualified to comment on the C implementation, so I'll defer to you and @rhettinger on that question :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, if people are deleting the func attribute from partial objects, I have a lot of questions about their broader approach to programming in general :)
this argument convinced me ;-). I just went with the isinstance
check in both places.
As a first approximation this looks good. In the next couple of days, I can devote some time to give it more thought. |
in
partial.__new__
, before checking for the existence of the attribute 'func', first check whether the argument is an instance of partial.