How to get method parameter names?

前端 未结 15 2419
眼角桃花
眼角桃花 2020-11-22 09:14

Given the Python function:

def a_method(arg1, arg2):
    pass

How can I extract the number and names of the arguments. I.e., given that I h

15条回答
  •  Happy的楠姐
    2020-11-22 09:38

    Update for Brian's answer:

    If a function in Python 3 has keyword-only arguments, then you need to use inspect.getfullargspec:

    def yay(a, b=10, *, c=20, d=30):
        pass
    inspect.getfullargspec(yay)
    

    yields this:

    FullArgSpec(args=['a', 'b'], varargs=None, varkw=None, defaults=(10,), kwonlyargs=['c', 'd'], kwonlydefaults={'c': 20, 'd': 30}, annotations={})
    

提交回复
热议问题