When writing a basic program today needed for fibonacci numbers, I came across the following situation to initialize two variables x & y at the same time.
I had originally written
x=1
y=2
......
x = y
y = x+y
Which obviously doesnt work to initialize both at the same time because because x becomes 2 leading to y becoming 4, which doesn't follow the the fibonacci sequence.
It had to be replaced with the following (that I didn't know was possible before searching):
x,y = y,x+y
And thus this produces the right sequence (x=2 and y =3)
My question is why this works? Or more specifically, what is happening with the memory allocation of variables underlying the syntax of that line of code. Are we telling the compiler to change y=x+y using x's previous value before its changed to y's previous value?
Thanks for all the help :)