与在C或C ++中使用varargs的方式类似:
C
C ++
varargs
fn(a, b) fn(a, b, c, d, ...)
是。
如果你不理会关键字参数,这很简单并且可以工作:
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将为你提供一个包含所有参数的元组。
对于关键字参数,你需要将其作为单独的实际参数接受,如Skurmedel的answer所示。
Skurmedel
answer