[SOLVED] How can we transform `functools.partial` into a decorator?

Issue

This Content is from Stack Overflow. Question asked by Samuel Muldoon

There is a tool in the functools library named partial.

functools.partial allows us to specify some of the inputs a function without specifying all of them.

You can transform …
funky_function(x1, x2, x3, x4, x5)
into something like…
funky_function(1, 2, x3, x4, x5)

That is…

  • Input argument x1 gets assigned the number 1
  • Input argument x2 gets assigned the number 2
  • Inputs x3, x4, x5 are still not assigned anything.
from functools import *
from inspect import *

def funky_function(x1, x2, x3, x4, x5, /):
    return ", ".join(" ".join(str(x).split()) for x in [x1, x2, x3, x4, x5])

funky_function = partial(funky_function, 1, 2)

result = funky_function(3, 4, 5)

print("return value from `funky_function(3, 4, 5)` is:", result)
print("signature of `funky_function` is:", signature(funky_function))

My question is, how do we make a decorator?

@partialize(1, 2)
def funky_function(x1, x2, x3, x4, x5, /):
    return ", ".join(" ".join(str(x).split()) for x in [x1, x2, x3, x4, x5])

result = funky_function(3, 4, 5)

Look at the function signature below of partial.partial(func):

functools.partial(func, /, *args, **keywords)

The function is the left and the arguments on the right.

The following two pieces of code are equivalent:

@decorate(1, 2)
def f():
   pass
###################################
def f():
   pass
f = decorate(1, 2)(f)

So, partial is not a good decorator if you want to enter arguments first, and enter the function second.

How do we make a decorator which takes in the arguments first, and later accepts the function to-be-decorated?



Solution

Something like the following works:

from functools import *
from inspect import *

def partialize(*args, **kwargs):
    def partialize_helper(old_func):
        new_func = partial(old_func, *args, **kwargs)
        return new_func
    return partialize_helper

@partialize(1, 2)
def funky_function(x1, x2, x3, x4, x5, /):
    return ", ".join(" ".join(str(x).split()) for x in [x1, x2, x3, x4, x5])

##########################################################################

funky_function = partialize(1, 2)(funky_function)

##########################################################################

result = funky_function(3, 4, 5)
print("valued returned by funky_function(3, 4, 5)".ljust(50), result)
print("str(funky_function) ==".ljust(50), str(funky_function))
print("Signature == ".ljust(50), signature(funky_function))

Console output is as follows:

valued returned by funky_function(3, 4, 5)         1, 2, 3, 4, 5
str(funky_function) ==                             functools.partial(<function funky_function at 0x000001C99759E200>, 1, 2)
Signature ==                                       (x3, x4, x5, /)


This Question was asked in StackOverflow by Samuel Muldoon and Answered by Samuel Muldoon It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?