This questions stems from PEP 448
-- Additional Unpacking Generalizations and is present in Python 3.5 as far as I'm aware (and not back-ported to 2.x
). Specifically, in the section Disadvantages, the following is noted:
Whilst
*elements, = iterable
causeselements
to be alist
,elements = *iterable
, causeselements
to be atuple
. The reason for this may confuse people unfamiliar with the construct.
Which does indeed hold, for iterable = [1, 2, 3, 4]
, the first case yields a list
:
>>> *elements, = iterable
>>> elements
[1, 2, 3, 4]
While for the second case a tuple
is created:
>>> elements = *iterable,
>>> elements
(1, 2, 3, 4)
Being unfamiliar with the concept, I am confused. Can anyone explain this behavior? Does the starred expression act differently depending on the side it is on?