如何在Python中解压缩参数?

时间:2019-05-07 00:15:46

标签: python function arguments argument-unpacking

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

def foo([ arg ]):
    pass

foo([ 42 ])

1 个答案:

答案 0 :(得分:1)

参数解压缩为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 ] )