1

In python 3.5.5 interpreter, this seems valid

>>> [] = ()
>>>

While this throws a SyntaxError

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

Why is the first statement valid and what does it mean ?

Why is the second one invalid ?

dis does not give a lot of informations:

dis.dis('[] = ()')   1           0 BUILD_TUPLE              0
              3 UNPACK_SEQUENCE          0
              6 LOAD_CONST               0 (None)
              9 RETURN_VALUE
NanoPish
  • 1,379
  • 1
  • 19
  • 35
  • Might be a bug, rather than intended behavior. I'm on 3.6.3 and `() = []` works on my machine. – Kevin Jul 26 '18 at 15:03
  • Both work fine on Python 3.7.0 – c2huc2hu Jul 26 '18 at 15:03
  • `()=[]` and `[]=()` both work fine in Python 3.7 when I try them (in that they do nothing). – khelwood Jul 26 '18 at 15:04
  • Can confirm the behaviour for Python 2.7 – tobias_k Jul 26 '18 at 15:04
  • Tried on python 3.5 and confirm the behavior. – scharette Jul 26 '18 at 15:05
  • 1
    In general that syntax is _supposed_ to work because of unpacking: you can assign to a tuple full of variables, and the variables receive the values unpacked from the right hand side of the assignment. It doesn't really mean anything for an empty tuple, so it's not that important whether it works or not. – khelwood Jul 26 '18 at 15:05
  • 3
    The `SyntaxError` in earlier versions is a minor bug, see https://bugs.python.org/issue23275 – jonrsharpe Jul 26 '18 at 15:06

0 Answers0