命名这个 python/ruby 语言结构(使用数组值来满足函数参数) [英] Name this python/ruby language construct (using array values to satisfy function parameters)
问题描述
这种语言结构叫什么?
在 Python 中我可以说:
In Python I can say:
def a(b,c): return b+c
a(*[4,5])
并得到 9.同样在 Ruby 中:
and get 9. Likewise in Ruby:
def a(b,c) b+c end
a(*[4,5])
当将单个数组传递给需要多个参数的函数时,这称为什么?
What is this called, when one passes a single array to a function which otherwise requires multiple arguments?
*
运算符的名称是什么?
What is the name of the *
operator?
还有哪些语言支持这个很酷的功能?
What other languages support this cool feature?
推荐答案
Python 文档称之为 解压参数列表.这是一个非常方便的功能.在 Python 中,您还可以使用双星号 (**) 将字典(哈希)解包为关键字参数.它们也反向工作.我可以定义这样的函数:
The Python docs call this Unpacking Argument Lists. It's a pretty handy feature. In Python, you can also use a double asterisk (**) to unpack a dictionary (hash) into keyword arguments. They also work in reverse. I can define a function like this:
def sum(*args):
result = 0
for a in args:
result += a
return result
sum(1,2)
sum(9,5,7,8)
sum(1.7,2.3,8.9,3.4)
将所有参数打包到一个任意大小的列表中.
To pack all arguments into an arbitrarily sized list.
这篇关于命名这个 python/ruby 语言结构(使用数组值来满足函数参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!