No swap function in Python?

Bolton, Gawain [ADC:4808:EXCH] boltong at
Wed May 30 16:57:41 CEST 2001

There doesn't seem to be a "swap" function in Python two swap the values
of two variables.   I was wondering why this is.

Unfortunately writing a generic swap function swap(a,b) with no return
type in Python doesn't work with immutable arguments (like strings) of
course.  Which made me think that a swap could be done like this:

(a,b) = (b,a)

But I'm not completely convinced doing this is safe.  In tests I've done
it works, but I'm not sure whether this works in all cases with all
types...  Finally, if the behaviour is guaranteed, is this an efficient
way of doing a swap?

Thanx in advance,


Gawain Bolton                     | E-mail: boltong at
Section 4808                      | Internal mail stop: BA54
UMTS Development                  |
Nortel Networks                   | Voice:  ESN 579-3763   +33
Guyancourt, France                | FAX:    ESN 579-3009   +33

More information about the Python-list mailing list