Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does star unpacking in python give a list

Tags:

python-3.x

I am curious on why python3's star unpacking returns me an instance of a list as compared to a tuple (which is returned by an asterisk in function argument). Is this a python's idiosyncrasy, or there is a good reason behind this.

In [1]: def foo(*args):
   ...:     print(type(args))
   ...:     

In [2]: foo(1, 2, 3, 4)
<class 'tuple'>

In [3]: first, *rest = (1, 2, 3, 4)

In [4]: type(rest)
Out[4]: list
like image 452
skgbanga Avatar asked Mar 06 '26 11:03

skgbanga


1 Answers

They figured a list would be easier to process:

After a short discussion on the python-3000 list [1], the PEP was accepted by Guido in its current form. Possible changes discussed were:

  • ...
  • Make the starred target a tuple instead of a list. This would be consistent with a function's *args, but make further processing of the result harder.
like image 99
user2357112 supports Monica Avatar answered Mar 08 '26 04:03

user2357112 supports Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!