1

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 Answer 1

2

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed we don't want end users calling arbitrary functions. What I am trying to do is use the python data model as a kind of database telling us how to map user requests to back-end functionality. But the two things are so similar, that I shouldn't be surprised if python doesn't support it. I'll accept your answer soon if I don't see a better one.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.