This line of code works by creating a tuple, and then destructuring it to assign two variables at once. The result is that both a
and b
on the left-hand-side are assigned the results of expressions on the right-hand-side which are calculated using the original values of a
and b
.
The expression on the right-hand-side is b, a + b
which is equivalent to (b, a + b)
, i.e. it creates a tuple with two components.
The assignment target on the left-hand-side is a, b
which is equivalent to (a, b)
, i.e. it assigns to two variables simultaneously, using the values of the two components from the tuple respectively. This is called a destructuring assignment.
This is convenient because if the two assignments were written separately, as in the code below, then it would not have the desired effect. The first line changes a
's value, and then the new value would (incorrectly) be used to calculate b
:
# WRONG!
a = b
b = a + b