Порядок множественного присвоения и оценки в PythonPython

Программы на Python
Гость
Порядок множественного присвоения и оценки в Python

Сообщение Гость »


Предположим, что у нас есть:

Код: Выделить всё

>>> x = 1
>>> y = 2
If we try assigning both values at once like so:

Код: Выделить всё

>>> x, y = y, x+y
>>> x
2
>>> y
3
Then we get a different result than if we did the assignments separately:

Код: Выделить всё

>>> x = 1
>>> y = 2
>>> x = y
>>> y = x+y
>>> x
2
>>> y
4
Why does this happen?

See also Multiple assignment semantics regarding the effect and purpose of parentheses on the left-hand side of a multiple assignment.
See also Understand Python swapping: why is a, b = b, a not always equivalent to b, a = a, b? for more complex cases, where the order of assignment matters.


Источник: https://stackoverflow.com/questions/872 ... -in-python

Вернуться в «Python»