5

I'm looking for a way to get parameters of a function into a dict containing their name and default value is any.

I've seen that the inspect module has a getcallargs function but it can raise exception and expects arguments to be provided if required by the inspected function. I am seeking something to access to this result without any prior knowledge of the function.

This must be compatible with python 2.7.

def a_function_somewhere(arg1, arg2=None, arg3=12):
    pass

r = the_function_i_m_looking_for(a_function_somewhere)
# Expected r {'arg1': Special.NoDefaultValue, 'arg2': None, 'arg3': 12}
2
  • Out of curiosity: what is the use-case? Commented Mar 20, 2014 at 8:19
  • I'm loading some code that's provided by the user of my library, and I would like to be able to pass to the provided functions elements according to their naming. That's not very explicit but I think it's the best solution. Commented Mar 20, 2014 at 16:24

1 Answer 1

5
>>> inspect.getargspec(a_function_somewhere)
ArgSpec(args=['arg1', 'arg2', 'arg3'],
        varargs=None, keywords=None, defaults=(None, 12))
Sign up to request clarification or add additional context in comments.

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.