Python **args和*kwargs错误

Python **args和*kwargs错误,python,arguments,Python,Arguments,我正在学习*args和**kwargs,有一个问题。当我们在列表中使用**,在字典中使用*时会发生什么?我知道它不起作用,但我想知道是语法问题还是有更直观的解释。让我们看看: >>> def f(arg): ... print(arg) ... >>> f(**[]) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError

我正在学习*args和**kwargs,有一个问题。当我们在列表中使用**,在字典中使用*时会发生什么?我知道它不起作用,但我想知道是语法问题还是有更直观的解释。

让我们看看:

>>> def f(arg):
...   print(arg)
... 
>>> f(**[])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() argument after ** must be a mapping, not list
>>> f(*{})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() missing 1 required positional argument: 'arg'
>>> f(*{'foo': 42})
foo
让我们来了解一下:

>>> def f(arg):
...   print(arg)
... 
>>> f(**[])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() argument after ** must be a mapping, not list
>>> f(*{})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() missing 1 required positional argument: 'arg'
>>> f(*{'foo': 42})
foo