fordy
fordy

Reputation: 2691

Wrapping arguments of functools.partial in other functions

I was wondering if it's possible to wrap args of partial() with other functions, or an alternative method that will achieve the same outcome. I'd like to do something which is possible in a verbose way like this:

from functools import partial
from module import assert_function

def my_helper_function(object):
    return do_something_to_object(object)     

my_assert = partial(assert_function, arg_to_change_default=False)

my_assert(my_helper_function(left), my_helper_function(right))

I wondered if something along the lines of the following was possible (this doesn't work, of course):

from functools import partial
from module import assert_function

def my_helper_function(object):
    return do_something_to_object(object)     

my_assert = partial(assert_function, left=my_helper_function(left), right=my_helper_function(right), arg_to_change_default=False)

my_assert(left, right)

Upvotes: 1

Views: 112

Answers (1)

wim
wim

Reputation: 362657

It's not possible directly with functools.partial. Function arguments are fully evaluated before calling the function, and functions can receive other functions as arguments.

Do this at call-time with a def instead.

Upvotes: 1

Related Questions