Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this unpacking expression not allowed in python3.10?

I used to unpack a long iterable expression like this:

In python 3.8.7:

>>> _, a, (*_), c = [1,2,3,4,5,6]
>>> a
2
>>> c
6

In python 3.10.7:

>>> _, a, (*_), c = [1,2,3,4,5,6]
  File "<stdin>", line 1
    _, a, (*_), c = [1,2,3,4,5,6]
           ^^
SyntaxError: cannot use starred expression here

I'm not sure which version of python between 3.8.7 and 3.10.7 introduced this backwards breaking behavior. What's the justification for this?

like image 277
OneRaynyDay Avatar asked Aug 02 '26 07:08

OneRaynyDay


1 Answers

There's an official discussion here. The most relevant quote I can find is:

Also the current behavior allows (*x), y = 1 assignment. If (*x) is to be totally disallowed, (*x), y = 1 should also be rejected.

I agree.

The final "I agree" is from Guido van Rossum.

The rationale for rejecting (*x) was:

Honestly this seems like a bug in 3.8 to me (if it indeed behaves like this):

>>> (*x), y (1, 2, 3)

Every time I mistakenly tried (*x) I really meant (*x,), so it's surprising that (*x), y would be interpreted as (*x, y) rather than flagging (*x) as an error.

Please don't "fix" this even if it is a regression.

Also by Guido van Rossum. So it seems like (*x) was rejected because it looks too similar to unpacking into a singlet tuple.

like image 116
Carcigenicate Avatar answered Aug 04 '26 21:08

Carcigenicate