How to get method parameter names?

前端 未结 15 2132
眼角桃花
眼角桃花 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条回答
  •  既然无缘
    2020-11-22 09:59

    What about dir() and vars() now?

    Seems doing exactly what is being asked super simply…

    Must be called from within the function scope.

    But be wary that it will return all local variables so be sure to do it at the very beginning of the function if needed.

    Also note that, as pointed out in the comments, this doesn't allow it to be done from outside the scope. So not exactly OP's scenario but still matches the question title. Hence my answer.

提交回复
热议问题