Extended unpacking in python3

I create a list as:

>>> seq = [1, 2, 3, 4]

Now I will assign the variables as follows:

>>> a, b, c, d, *e = seq
>>> print(a, b, c, d, e)

I get output like:

>>> 1 2 3 4 []

Now I am changing the sequence in which I assign the variables as:

>>> a, b, *e, c, d = seq
>>> print(a, b, c, d, e)

I get output like:

>>> 1, 2, 3, 4, []

So my question is: why is the * e variable always assigned an empty list no matter where it appears?

+4
source share
3 answers

It was a design according to PEP 3132 (with my bold):

( ) ( ) , ( "" , "" ). , , , , , .

, PEP :

>>> a, *b, c = range(5)
>>> a
0
>>> c
4
>>> b
[1, 2, 3]
+4

a, b, c, d, *e = seq

, a, b, c d , e. , e - .

a, b, *e, c, d = seq

a b. *e . , . seq . .

+4
a, b, *e, c, d = [1, 2, 3, 4]

*e " e". 4 , e.

+2

Source: https://habr.com/ru/post/1617727/


All Articles