Module inspect
from Python’s standard library is your friend — see the online docs! inspect.getargspec(func)
returns a tuple with four items, args, varargs, varkw, defaults
: len(args)
is the “primary arity”, but arity can be anything from that to infinity if you have varargs
and/or varkw
not None
, and some arguments may be omitted (and defaulted) if defaults
is not None
. How you turn that into a single number, beats me, but presumably you have your ideas in the matter!-)
This applies to Python-coded functions, but not to C-coded ones. Nothing in the Python C API lets C-coded functions (including built-ins) expose their signature for introspection, except via their docstring (or optionally via annotations in Python 3); so, you will need to fall back to docstring parsing as a last ditch if other approaches fail (of course, the docstring might be missing too, in which case the function will remain a mystery).