Sunday, 15 September 2013

python - Get function arguments which are not default -



python - Get function arguments which are not default -

i have function:

def func(a, b, c=1, d=2): pass

with this:

import inspect inspect.getargspec(func)

i result:

argspec(args=['a', 'b', 'c', 'd'], varargs=none, keywords=none, defaults=(1, 2))

is there easier way find arguments of function not take default values? statement above have ugly this

ab = args[:-len(defaults)]

to retrieve arguments. ab ['a', 'b']

thanks.

[edit] more clarifications: want inspect outside function func, not within it.

for python 3.3 , newer using signature:

in [15]: [x x, y in inspect.signature(func).parameters.items() if y.default y.empty] out[15]: ['a', 'b']

python

No comments:

Post a Comment