Been looking (again) at XMPP recently. While browsing through existing source code and samples in several languages, there’s one pattern which comes back quite frequently in ‘echobot’ demos: when a message comes in, the to and from attributes are swapped, and the message is sent.
The most common approach is something like (pseudocode):
temp = from
from = to
to = temp
In Python there’s an easier approach though which seems to be unknown to several developers. It uses the multi-assignment/expansion syntax:
from, to = to, from
Basically, the tuple on the right (to, from) is constructed, then expanded to locals ‘from’ and ‘to’.
Just a hint It’s a pretty elegant line of code IMHO.