可能的相关问题(我搜索了相同的问题,但没有找到)
如https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists 中所述,Python 提供了一种使用星号将参数解包为函数的便捷方法
>>> list(range(3, 6)) # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> list(range(*args)) # call with arguments unpacked from a list
[3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我正在调用这样的函数:
def func(*args):
for arg in args:
print(arg)
Run Code Online (Sandbox Code Playgroud)
在 Python 3 中,我这样称呼它:
a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
func(*a, *b, *c)
Run Code Online (Sandbox Code Playgroud)
哪些输出
1 2 3 4 5 6 7 8 9
Run Code Online (Sandbox Code Playgroud)
然而,在 Python 2 …