Reputation: 1
In newer versions, ROT_TWO is replaced by SWAP.
However, after disassembling the function using dis.dis()
, I noticed that SWAP
does not appear in the bytecode.
Python 3.9.0
>>> import dis
>>> def foo(a, b):
... a, b = b, a
...
>>> dis.dis(foo)
2 0 LOAD_FAST 1 (b)
2 LOAD_FAST 0 (a)
4 ROT_TWO
6 STORE_FAST 0 (a)
8 STORE_FAST 1 (b)
10 LOAD_CONST 0 (None)
12 RETURN_VALUE
>>>
Python 3.11.0
>>> import dis
>>> def foo(a, b):
... a, b = b, a
...
>>> dis.dis(foo)
1 0 RESUME 0
2 2 LOAD_FAST 1 (b)
4 LOAD_FAST 0 (a)
6 STORE_FAST 1 (b)
8 STORE_FAST 0 (a)
10 LOAD_CONST 0 (None)
12 RETURN_VALUE
>>>
Further Reading:
How does swapping of members in tuples (a,b)=(b,a) work internally?
Upvotes: 0
Views: 38