In Python, I’ve seen two variable values swapped using this syntax:
left, right = right, left
Is this considered the standard way to swap two variable values or is there some other means by which two variables are by convention most usually swapped?
That means the following for the expression
a,b = b,a:b,ais evaluated, that is to say, a tuple of two elements is created in the memory. The two elements are the objects designated by the identifiersbanda, that were existing before the instruction is encountered during the execution of the program.abe assigned to the first element of the tuple (which is the object that was formerly b before the swap because it had nameb)and the second identifier
bis assigned to the second element of the tuple (which is the object that was formerly a before the swap because its identifiers wasa)This mechanism has effectively swapped the objects assigned to the identifiers
aandbSo, to answer your question: YES, it’s the standard way to swap two identifiers on two objects.
By the way, the objects are not variables, they are objects.