def do_something(*args): # do something do_something(1,2,3,4,5,...) # arbitrarily long list
我正在阅读PHP手册,它说:
PHP 4 and above has support for
variable-length argument lists in
user-defined functions. This is really
quite easy,using the func_num_args(),
func_get_arg(),and func_get_args()
functions.No special Syntax is required,and
argument lists may still be explicitly
provided with function definitions and
will behave as normal.
我得到了第一部分.您可以将任意数量的参数传递给不带参数的函数,然后使用func_get_args()等将它们作为数组传递.但是,我并没有真正得到第二部分所说的内容.
所以,我的问题是,是否有一些特殊的可变长度参数语法,或者一些我不知道的最佳实践?手册建议的方法充其量只是kludgey并使你的功能似乎没有参数(除非我做错了).我根本不应该尝试使用这种语言功能吗?