There is this code:
>>> (a, b) = (2, 3)
>>> a
2
>>> b
3
Why variables a and b are alive after tuple creation? I mean that here:
(a, b) = (2, 3)
is created some tuple and this tuple is not assigned to any variable so garbage collector should immediately destroy this tuple after this line.
Variables a and b are only references by this tuple – so if this tuple (a, b) is destroyed then variables a and b should be only destroyed also.
So why these variables still exist after definition of tuple?
The parser doesn’t see
(a, b)as a tuple, although it does do tuple unpacking for you. Thus, there is no tuple to create, let alone destroy. Instead, python sees this as two separate variablesaandb.You can see this if you were to disassemble the compiled bytecode for the statement:
The constant
(2, 3)is unpacked, then stored into the local variablesaandb.