有没有办法将命名函数参数转换为字典

Is there a way to convert named function arguments to dict

我想知道是否有办法将命名参数转换为 dict

我知道使用 **kwargs 代替单个命名参数会非常简单。

def func(arg1=None, arg2=None, arg3=None):
    # How can I convert these arguments to {'arg1': None, 'arg2': None, 'arg3': None}`

您可以使用 locals() 获取本地参数:

def func(arg1=None, arg2=None, arg3=None):
    print(locals())

func()  # {'arg3': None, 'arg2': None, 'arg1': None}