10

I am looking to do something similar to what was asked here Getting list of parameter names inside python function, but using partial functions. ie. I need to get the possible arguments for a partial function. I can get the keyword arguments using:

my_partial_func.keywords

but I am currently struggling to get the non-keyword arguments. The inspect module also yields nothing for this particular case. Any suggestions would be great.

2 Answers 2

8

.args contains the arguments passed to the partial function. If you want to get the arguments that the original function expects, use the inspect solution you linked on the .func attribute.

You can find this out by calling dir on a functools.partial object:

>>> dir(x)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'args', 'func', 'keywords']
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, calling inspect on the my_partial_function.func worked
0

You can use partial.func.__code__.co_varnames:

import functools

p = functools.partial(lambda a, b: a > b, {a: 1})

print(p.args)
# ({'a': 1},)

print(p.func.__code__.co_varnames)
# ('a', 'b')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.