如何在Python中解压缩参数?

皮埃尔·邓恩

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

def foo([ arg ]):
    pass

foo([ 42 ])
帕特里克·豪

在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 ] )

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章