Some people say “you learn something new everyday” or something like that. Today someone on #python showed me a cool trick I never would have thought of on my own.
Often there is a time when you want to swap the contents of two variables. The most popular way to do this is using a third variable as shown below:
temp = a
a = b
b = temp
This looks sucky and doesn’t really express very well what you want to do. A much better way to do this in Python is with the following magic line:
a, b = b, a
Doesn’t that look so much better? And it is very clear to anyone who has used Python before what is going on. To think, I have been using Python for about 7 years now and never thought of doing that.
Just thought I would share this tidbit.