2 ms·
It’s a good point that Python is smart enough not to build a mutable list on the right-hand side when not needed (e.g. for "x = [1, 2]" it will and for "y, z =
by makecheck 9y ago
It’s a good point that Python is smart enough not to build a mutable list on the right-hand side when not needed (e.g. for "x = [1, 2]" it will and for "y, z = [1, 2]" it will not). A right-hand tuple however is still a separate type, and if you disassemble the original cases you can see that "[] = []" is more wasteful than "[] = ()".
>>> import dis
>>>
>>> @dis.dis
... def f():
... a, b = 1, 2
... (a, b) = [2, 3]
... (a,) = [4]
... a, = [5]
... [] = []
... [] = ()
...
3 0 LOAD_CONST 6 ((1, 2))
3 UNPACK_SEQUENCE 2
6 STORE_FAST 0 (a)
9 STORE_FAST 1 (b)
4 12 LOAD_CONST 2 (2)
15 LOAD_CONST 3 (3)
18 ROT_TWO
19 STORE_FAST 0 (a)
22 STORE_FAST 1 (b)
5 25 LOAD_CONST 4 (4)
28 STORE_FAST 0 (a)
6 31 LOAD_CONST 5 (5)
34 STORE_FAST 0 (a)
7 37 BUILD_LIST 0
40 UNPACK_SEQUENCE 0
8 43 LOAD_CONST 7 (())
46 UNPACK_SEQUENCE 0
49 LOAD_CONST 0 (None)
52 RETURN_VALUE