2

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

def foo([ arg ]):
    pass

foo([ 42 ])
4

1 回答 1

2

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 ] )
于 2019-05-07T00:21:32.423 回答