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?

Community
  • 1
  • 1
Adrian Ratnapala
  • 5,485
  • 2
  • 29
  • 39

1 Answers1

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

Amber
  • 507,862
  • 82
  • 626
  • 550
  • 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. – Adrian Ratnapala Jan 27 '13 at 09:29