The key to understanding this is that everything to the right of =
is executed first by the language, and then it does the assignments -
So while doing
a, b = b, a + b
(which is the same as:
a, b = (b, a + b)
Python dispenses parentheses if there is no ambiguity)
What happens is that an object of type tuple is created with the current value of b, and the value of a + b
as its two elements. This tuple is temporarily in memory as a result of the expression - and then these values are distributed and assigned respectively to and to b.
If you do:
a = b
On a separate line, the value of a
has already been overwritten by doing a + b
on another line (and the language would not be usable if you did something like this - it would be completely unpredictable).
Remember that this language to calculate a tuple and associate its values in a same line is used, because without this feature you would need a third variable just to make this exchange - the code in most other languages, which does not have this feature must be:
c = a
a = b
b = c + b
If you write something like this:
a, b = inspecionar = b, a+b
print(a, b, inspecionar)
The language, in addition to distributing the values of the tuple teporaria to a
and b
, will save the entire tuple in the inspecionar
variable. This% w / w% more will help you understand what is happening.