在Python中是否可以像JavaScript一样解开参数?
def foo([ arg ]):
pass
foo([ 42 ])
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 ] )
def foo(arg, *arg2):
这个函数,它会将arg2
解包成一个列表。 - chickity china chinese chicken