How to get method parameter names?

前端 未结 15 2161
眼角桃花
眼角桃花 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:44

    The Python 3 version is:

    def _get_args_dict(fn, args, kwargs):
        args_names = fn.__code__.co_varnames[:fn.__code__.co_argcount]
        return {**dict(zip(args_names, args)), **kwargs}
    

    The method returns a dictionary containing both args and kwargs.

提交回复
热议问题