In Python, I\'ve seen two variable values swapped using this syntax:
left, right = right, left
Is this considered the standard way to swap
To get around the problems explained by eyquem, you could use the copy
module to return a tuple containing (reversed) copies of the values, via a function:
from copy import copy
def swapper(x, y):
return (copy(y), copy(x))
Same function as a lambda
:
swapper = lambda x, y: (copy(y), copy(x))
Then, assign those to the desired names, like this:
x, y = swapper(y, x)
NOTE: if you wanted to you could import/use deepcopy
instead of copy
.