1

This issue is really confusing for me. What is actually happening behind the curtains here? Why doesn't the latter work?

>>> [] = ''
>>> () = ''
  File "<stdin>", line 1
SyntaxError: can't assign to ()

My theory was that there has to be an object or iterable object on the right and its value/s are assigned to the number of variables from the left side container. This thought was based on:

>>> [a] = '1'
>>> [a,b] = '12'
>>> a
'1'
>>> b
'2'
>>> [a,b] = '123'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
>>> [a] = ''
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 0 values to unpack

Which also works the same on tuples:

>>> (a,) = '1'
>>> (a,b) = '12'
>>> a
'1'
>>> b
'2'
>>> (a,b) = '123'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
>>> (a,) = ''
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 0 values to unpack

But there is something that I miss because this fails:

>>> [] = ''
>>> () = ''
  File "<stdin>", line 1
SyntaxError: can't assign to ()

What is going on that breaks the latter assignment?

rbs
  • 169
  • 4
  • 10

0 Answers0