Home > Backend Development > Python Tutorial > How does Python swap tuple values without using a temporary variable?

How does Python swap tuple values without using a temporary variable?

Mary-Kate Olsen
Release: 2024-11-28 09:07:10
Original
311 people have browsed it

How does Python swap tuple values without using a temporary variable?

Swapping Values in Tuples: A Peek Inside

In Python, swapping the values of two variables using tuple assignment like (a, b) = (b, a) might seem like a simple trick, but how does it work internally without a temporary variable?

Stack Manipulation

Python evaluates the right-hand side expression of the assignment separately from the left-hand side. Once the right-hand side is computed, its result is stored on the stack. Then, the left-hand side names are assigned using opcodes that operate directly on the stack.

For tuple assignments with two or three items, Python utilizes the stack directly using ROT_TWO or ROT_THREE opcodes to swap elements. For instance:

def foo(a, b):
    a, b = b, a
Copy after login

Disassembling this function using dis.dis(foo) reveals the following:

  2           0 LOAD_FAST                1 (b)
              3 LOAD_FAST                0 (a)
              6 ROT_TWO             
              7 STORE_FAST               0 (a)
             10 STORE_FAST               1 (b)
             13 LOAD_CONST               0 (None)
             16 RETURN_VALUE        
Copy after login

Here, ROT_TWO swaps the top two values on the stack, effectively reversing the order of a and b.

Tuple Construction and Unpacking

For assignments involving more than three elements, Python constructs an intermediate tuple on the stack. Consider this example:

def bar(a, b, c, d):
    d, c, b, a = a, b, c, d
Copy after login

Its disassembly shows:

  2           0 LOAD_FAST                0 (a)
              3 LOAD_FAST                1 (b)
              6 LOAD_FAST                2 (c)
              9 LOAD_FAST                3 (d)
             12 BUILD_TUPLE              4
             15 UNPACK_SEQUENCE          4
             18 STORE_FAST               3 (d)
             21 STORE_FAST               2 (c)
             24 STORE_FAST               1 (b)
             27 STORE_FAST               0 (a)
             30 LOAD_CONST               0 (None)
             33 RETURN_VALUE        
Copy after login

BUILD_TUPLE builds a tuple from the values on the stack in reverse order. Then, UNPACK_SEQUENCE pops the tuple and places its elements back onto the stack for assignment to the left-hand side variables.

Optimization

While UNPACK_SEQUENCE appears redundant for two or three-item assignments, a later optimization step replaces the BUILD_TUPLE/UNPACK_SEQUENCE combo with the more efficient ROT_TWO or ROT_THREE opcodes, ensuring that the swapping mechanism is as streamlined as possible.

The above is the detailed content of How does Python swap tuple values without using a temporary variable?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template