The key to understanding this is that everything on the right of the =
is executed before by the language, and then she makes the assignments -
then while doing
a, b = b, a + b
(which is the same as :
a, b = (b, a + b)
Python dispenses with 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 tpla is temporarily in memory as a result of the expression - and then these values are distributed and assigned respectively to a and to b.
If you do:
a = b
in a separate line, the value of a
has been over-written in making a + b
in another line (and the language would not be usable if it did something like this - it would be completely unpredictable).
It is worth remembering that this language of calculating a tuple and associating its values in the same line is used, because without this resource you would need a third variable just to make this exchange - the code in most other languages, which does not have this feature has to be:
c = a
a = b
b = c + b
If you write something like that:
a, b = inspecionar = b, a+b
print(a, b, inspecionar)
the language, in addition to distributing the values of the teporária tuple to a
and b
, will store the entire tuple in the variable inspecionar
. That one print
the more will help you understand what is happening.