11
In the question Inverting two variables without using a temporary an answer quotes a link that comments on the multiple assignment of variables in Python:
Functioning of Multiple Assignment
The multiple assignment is the assignment of a tuple to another, where the values of the tuple to the right of the assignment signal, will be assigned, for the respective variables, in the double of the left side of the assignment signal. [sic]
In other references I have seen commenting similar, that the multiple assignment uses the deconstruction of tuples. I myself started from this premise to answer Why divide this operation into two cause result change?
In order to validate this information I went to analyze the bytecode:
from dis import dis
code = '''
a = 1
b = 3
a, b = b, a
'''
print(dis(code))
Who had a way out:
2 0 LOAD_CONST 0 (1)
2 STORE_NAME 0 (a)
3 4 LOAD_CONST 1 (3)
6 STORE_NAME 1 (b)
5 8 LOAD_NAME 1 (b)
10 LOAD_NAME 0 (a)
12 ROT_TWO
14 STORE_NAME 0 (a)
16 STORE_NAME 1 (b)
18 LOAD_CONST 2 (None)
20 RETURN_VALUE
None
That is, it loads in the stack the value of b
, stack the value of a
, exchange the two values inside the stack and make two assignments, first in a
, then in b
. There is no construction and deconstruction of tuples in the process, such as if I actually define a tuple with the two values:
from dis import dis
code = '''
a = 1
b = 3
c = b, a
'''
print(dis(code))
Which generates the following sequence of operations:
2 0 LOAD_CONST 0 (1)
2 STORE_NAME 0 (a)
3 4 LOAD_CONST 1 (3)
6 STORE_NAME 1 (b)
5 8 LOAD_NAME 1 (b)
10 LOAD_NAME 0 (a)
12 BUILD_TUPLE 2
14 STORE_NAME 2 (c)
16 LOAD_CONST 2 (None)
18 RETURN_VALUE
None
Where we can clearly see the operation BUILD_TUPLE
being executed before allocation.
After all, does multiple assignment use tuples in the process? If not, would it be a tuple-like assigment, only to illustrate which value is assigned to each variable?
Excellent question, accompanying....
– Luiz Augusto
In this Link, mainly in this excerpt "Multiple assignment is often called "tuple unpacking" because it’s Frequently used with tuples. But we can use Multiple assignment with any iterable, not just tuples. Here we’re using it with a list:".
– Luiz Augusto
@Luizaugusto It would be another example that might be wrong. A tuple unpacking that he cites would assign a tuple that already exists on the stack to variables. It would be like having
c = (1, 3)
and then doa, b = c
; here yes there is the deconstruction of tuple (tuple unpacking), evident by the operationUNPACK_SEQUENCE
. Multiple assignment does not occur.– Woss
See that he also quotes "What’s happening at a Lower level is that we’re Creating a tuple of...", contrary to the bytecode operations I posed in the question.
– Woss
Correct, and against himself. I debugged the code, which he mentions in the link,
x,y=10,20
. , I don’t have much experience with debug, but I haven’t found anybuild_tuple
orbuild_list
Is there any tuple construction or list in this code?– Luiz Augusto
@Luizaugusto In this case happens to
UNPACK_SEQUENCE
for the value10, 20
It’s a constant sequence. Instead of stacking two constants, Python already stacks the sequence with the two values and, for the assignment, deconstructs it.– Woss