python - 如何在 Python 中解压参数?

标签 python function arguments argument-unpacking

是否可以像在 javascript 中那样在 python 中解压参数?

def foo([ arg ]):
    pass

foo([ 42 ])

最佳答案

参数解包是removed in Python 3因为这很困惑。在 Python 2 中你可以这样做

def foo(arg, (arg2, arg3)):
    pass

foo( 32, [ 44, 55 ] )

Python 3 中的等效代码是

def foo(arg, arg2, arg3):
    pass

foo( 32, *[ 44, 55 ] )

def foo(arg, args):
    arg2, arg3 = args

foo( 32, [ 44, 55 ] )

关于python - 如何在 Python 中解压参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56013914/

相关文章:

python - 将记录添加到 numpy 记录数组

Python/ Pandas : Shift entities of a row to the right (end)

python - MapReduce Amazon Python 获取输入文件的行号

scala - 重载的方法值在scala中应用替代错误

python - Julia:将 Dict 项作为参数传递给函数

python pandas - 根据包含字符串列表的 B 列更改 A 列中的值

javascript - 全局作用域的函数表达式

javascript - 使用 Javascript 函数将 HTML 写入屏幕

JavaScript - 创建多个未知参数并将其从字符串传递到函数

arrays - 在批处理脚本中将数组作为参数传递