How to get method parameter names?

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

    inspect.signature is very slow. Fastest way is

    def f(a, b=1, *args, c, d=1, **kwargs):
       pass
    
    f_code = f.__code__
    f_code.co_varnames[:f_code.co_argcount + f_code.co_kwonlyargcount]  # ('a', 'b', 'c', 'd')
    

提交回复
热议问题