类似于在 C 或 C++ 中使用可变参数:
fn(a, b) fn(a, b, c, d, ...)
是的。您可以*args用作 非关键字 参数。然后,您将能够传递任意数量的参数。
*args
def manyArgs(*arg): print "I was called with", len(arg), "arguments:", arg >>> manyArgs(1) I was called with 1 arguments: (1,) >>> manyArgs(1, 2, 3) I was called with 3 arguments: (1, 2, 3)
如您所见,Python 会将参数 解压缩 为包含所有参数的单个元组。