Check python function signature without a call

你。 提交于 2019-12-10 11:53:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!