3

Is it possible to unpack parameters in python like in javascript?

def foo([ arg ]):
    pass

foo([ 42 ])

1 Answers1

3

Parameter unpacking was removed in Python 3 because it was confusing. In Python 2 you can do

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

foo( 32, [ 44, 55 ] )

The equivalent code in Python 3 would be either

def foo(arg, arg2, arg3):
    pass

foo( 32, *[ 44, 55 ] )

or

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

foo( 32, [ 44, 55 ] )
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • Why they removed such a good part of a language? "Because it was confusing"? Really? If it confuses someone, why they just do not use this feature? – Monsieur Pierre Doune May 07 '19 at 00:26
  • The reasons are [laid out in the PEP](https://www.python.org/dev/peps/pep-3113/#why-they-should-go). They boil down to: it makes introspection tools harder to write, the error messages are difficult to debug, and no one was really using it anyways. – Patrick Haugh May 07 '19 at 00:28
  • This is sad to see, coming from JavaScript. What a bad decision in my opinion. – Nano Miratus Sep 08 '21 at 02:25