AsTeR
AsTeR

Reputation: 7521

Reflection in Python: how to view all parameters of a function in Python 2.7

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}

Upvotes: 5

Views: 322

Answers (1)

jfs
jfs

Reputation: 414149

>>> inspect.getargspec(a_function_somewhere)
ArgSpec(args=['arg1', 'arg2', 'arg3'],
        varargs=None, keywords=None, defaults=(None, 12))

Upvotes: 5

Related Questions