Skip to content
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

Inconsistency in behavior of functools.partial and makefun.partial #95

Open
elchupanebrej opened this issue Jul 10, 2023 · 1 comment
Open

Comments

@elchupanebrej
Copy link

There is function

def func(a):
    print(f"Arg: {a}")

When it is used by functiools.partial:

from functools import partial

partial(partial(func, a=1), a=2)()
"""
>>> Arg: 2
"""

When it is used by makefun.partial:

from makefun import partial as mpartial

mpartial(mpartial(func, a=1), a=2)()
"""
>>> Arg: 1
"""

So makefun.partial could not be drop-in replacement for functools.partial

@smarie
Copy link
Owner

smarie commented Nov 9, 2023

Nice finding @elchupanebrej !!

I was not aware that partial could be used to modify assignments already done by a nested partial.

So makefun.partial could not be drop-in replacement for functools.partial

Well, in most applications it can be, as the usage you mention is extremely rare :)

If you would like to propose something, I am open to a PR modifying partial to

  • detect that f is an already partialized function (by us). We can rely on the existence of the func attribute but this is not enough.
  • update the dict in the appropriate closure if this is the case, rather than re-partialize it

Note that storing the preset_kwargs outside of the closure (i.e. on an explicit attribute on the function) could be a solution, but I have not enough knowledge of python internals to be sure that this would not impact performance or have side-effects...

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

No branches or pull requests

2 participants