1

I am trying to make sense of starred expressions in python. When I use it in python functions, it allows to call functions with different number of arguments:

def my_sum(*args):
    results = 0
    for x in args:
        results += x
    return results

print(my_sum(1,2,3))
>>6

print(my_sum(1,2,3,4,5,6,7))]
>>28

However when I use it in an assignment, it works like this:

a, b, *c, d = [1,2,3,4,5,6,7,8,9,10]
print(a,b,c,d)

>>1 2 [3, 4, 5, 6, 7, 8, 9] 10

*a, b, *c, d = [1,2,3,4,5,6,7,8,9,10]
print(a,b,c,d)

*a, b, *c, d = [1,2,3,4,5,6,7,8,9,10]
^
SyntaxError: multiple starred expressions in assignment

Can someone explain to me what is happening behind this assignment that doesn't allow multiple starred expressions?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
amiref
  • 3,181
  • 7
  • 38
  • 62
  • 2
    It's "extended iterable unpacking" referenced from e.g. [python - Star * operator on left vs right side of an assignment statement - Stack Overflow](https://stackoverflow.com/questions/35636785/star-operator-on-left-vs-right-side-of-an-assignment-statement) – user202729 Jun 19 '22 at 12:18
  • 2
    How would you divide the remaining items between the `a` and `c` lists in your last case?? – Thierry Lathuille Jun 19 '22 at 12:19
  • 4
    in first `a b d` takes one value, the rest goes in `c`. If there were multiple * , that would lead to several possiblities of repartition – azro Jun 19 '22 at 12:20

1 Answers1

2

The language doesn't allow this because it would be ambiguous, and allowing ambiguities is contrary to the Zen of Python:

In the face of ambiguity, refuse the temptation to guess.


Let's say you run:

*a, b, *c, d = [1,2,3,4,5,6,7,8,9,10]

One way to interpret that would be:

a = [1,2,3,4,5,6,7]
b = 8
c = [9]
d = 10

Another would be:

a = [1]
b = 2
c = [3,4,5,6,7,8,9]
d = 10

Yet another would be:

a = []
b = 1
c = [2,3,4,5,6,7,8,9]
d = 10

Python refuses to guess: It simply declares the code invalid.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Okay, makes sense; however, why does this lead to the same error? *a, b, c, *d = [1,2,3,4] There is no ambiguity here. – amiref Jun 20 '22 at 07:36
  • Why do you think there's no ambiguity there? `a` could have 0 elements or any number up to 8. `d` likewise. – Charles Duffy Jun 20 '22 at 10:41
  • if we do this assignment => `*a, b, c, *d = [1,2,3,4]` we enforce a=1, b=2, c=3, d=4, no? – amiref Jun 20 '22 at 11:34
  • 1
    No, we do not. It could be `a=[], b=1, c=2, d=[3,4]`; or it could be `a=[1,2]; b=3; c=4; d=[]`. Why would you think it couldn't be either of those things? – Charles Duffy Jun 20 '22 at 12:06
  • Aaah, okay, I did not think of an option when one of them is an empty array – amiref Jun 20 '22 at 12:36
  • 1
    @amiref, the other thing is that _at compile time_ Python doesn't know how many things will be on the right-hand side (except in the special case where the RHS is a constant). For it to take that into account to decide whether the assignment was ambiguous or not it would need to defer it to runtime, and throwing errors earlier is better than later. – Charles Duffy Jun 20 '22 at 13:10