Is there a way to check a function's signature in Python?

后端 未结 4 2364
我寻月下人不归
我寻月下人不归 2021-02-19 08:16

I\'m looking for a way to check the number of arguments that a given function takes in Python. The purpose is to achieve a more robust method of patching my classes for tests.

4条回答
  •  情书的邮戳
    2021-02-19 08:45

    You can use:

    import inspect
    len(inspect.getargspec(foo_func)[0])
    

    This won't acknowledge variable-length parameters, like:

    def foo(a, b, *args, **kwargs):
        pass
    

提交回复
热议问题