问题
My program derives a sequence args
and a mapping kwargs
from user input. I want to check that input, and then forward it to a python function f
(which is chosen based on user input). In this case, a function signature mismatch between f
and [kw]args
is an input error; I must distinguish it from possible programming errors within the implementation of f
, even though they might both raise TypeError
.
So I want to check the signature before attempting the function call. Is there a way to do this other than to manually compare [kw]args
to the result of inspect.getargspec
(or .getfullargspec
or .signature
in later python versions)?
Related questions: Is there a way to check a function's signature in Python?
回答1:
The method using inspect
is probably the most straightforward way of doing this that exists - it's not something one would normally expect to be doing in Python.
(Typically, allowing end users to call arbitrary functions with arbitrary inputs is not what a programmer wants.)
来源:https://stackoverflow.com/questions/14545857/check-python-function-signature-without-a-call