是否有python函数可以使用未指定数量的参数?

alw*_*btc 1 python arguments function unspecified

可以有python函数,其中包含未指定数量的参数,例如

myfunc(a, b, c)

myfunc(a, b, c, d, e)
Run Code Online (Sandbox Code Playgroud)

哪两个都有效?

ava*_*sal 7

myfunc(*args,**kw)

*args - 需要N个参数

**kw - 需要字典(未指定深度)

In [1]: def myfunc(*args):
   ...:     print args
   ...:

In [2]: myfunc(1)
(1,)

In [3]: myfunc(1,2,3,4,5)
(1, 2, 3, 4, 5)
Run Code Online (Sandbox Code Playgroud)

  • 对Python教程的引用在这里很有用,可能是这样的:看一下[定义函数]的Python文档(http://docs.python.org/tutorial/controlflow.html#more-on-defining-功能).特别是,请参阅[任意参数列表](http://docs.python.org/tutorial/controlflow.html#arbitrary-argument-lists)和[解压缩参数列表](http://docs.python.org)部分. /tutorial/controlflow.html#unpacking-argument-lists). (4认同)