I wouldn’t really call the left-hand side “transient”. a
and b
remain defined. There is no list, even briefly. The syntax is part of the assignment statement’s syntax, not an orthogonal combination of a list or tuple display and assignment. Note that set syntax is explicitly forbidden:
>>> {a, b} = [1,2]
File "<python-input-0>", line 1
{a, b} = [1,2]
^^^^^^
SyntaxError: cannot assign to set display here. Maybe you meant '==' instead of '='?
At least in CPython, you can see that the style of syntax used doesn’t affect the end result, which is just to unpack the RHS and perform a series of assignments to the targets on the LHS.
>>> import dis
>>> dis.dis("""a, b = [1,2]""")
0 RESUME 0
1 LOAD_CONST 0 (1)
LOAD_CONST 1 (2)
BUILD_LIST 2
UNPACK_SEQUENCE 2
STORE_NAME 0 (a)
STORE_NAME 1 (b)
RETURN_CONST 2 (None)
>>> dis.dis("""[a, b] = [1,2]""")
0 RESUME 0
1 LOAD_CONST 0 (1)
LOAD_CONST 1 (2)
BUILD_LIST 2
UNPACK_SEQUENCE 2
STORE_NAME 0 (a)
STORE_NAME 1 (b)
RETURN_CONST 2 (None)
You can see the different in the AST, though, for what it’s worth:
>>> import ast
>>> print(ast.dump(ast.parse("""a, b = [1,2]"""), indent=2))
Module(
body=[
Assign(
targets=[
Tuple(
elts=[
Name(id='a', ctx=Store()),
Name(id='b', ctx=Store())],
ctx=Store())],
value=List(
elts=[
Constant(value=1),
Constant(value=2)],
ctx=Load()))])
>>> print(ast.dump(ast.parse("""[a, b] = [1,2]"""), indent=2))
Module(
body=[
Assign(
targets=[
List(
elts=[
Name(id='a', ctx=Store()),
Name(id='b', ctx=Store())],
ctx=Store())],
value=List(
elts=[
Constant(value=1),
Constant(value=2)],
ctx=Load()))])
A different implementation could, in theory, choose to erase the difference during parsing, or preserve the difference until a
and b
are defined. But the grammar is pretty specific in distinguishing a start_atom
used to define the targets from an expression.