No swap function in Python?

Bolton, Gawain [ADC:4808:EXCH] boltong at europem01.nt.com
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

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






More information about the Python-list mailing list