No swap function in Python?
Thomas Wouters
thomas at xs4all.net
Wed May 30 11:33:28 EDT 2001
On Wed, May 30, 2001 at 04:57:41PM +0200, Bolton, Gawain [ADC:4808:EXCH] wrote:
> There doesn't seem to be a "swap" function in Python two swap the values
> of two variables. I was wondering why this is.
Because it isn't necessary.
> 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.
It is.
> 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?
Yes.
--
Thomas Wouters <thomas at xs4all.net>
Hi! I'm a .signature virus! copy me into your .signature file to help me spread!
More information about the Python-list
mailing list