No swap function in Python?
Gawain Bolton
boltong at nortelnetworks.com
Thu May 31 04:13:21 EDT 2001
Nick Perkins wrote:
> "(a,b) = (b,a)" compiles to (approximately):
> >
> > LOAD_FAST b
> > LOAD_FAST a
> > BUILD_TUPLE 2
> > UNPACK_TUPLE 2
> > STORE_FAST a
> > STORE_FAST b
> >
> > It's easy to see how you can use the runtime stack without going through
> the
> > BUILD/UNPACK pair:
> >
> > LOAD_FAST b
> > LOAD_FAST a
> > STORE_FAST b
> > STORE_FAST a
> >
> > Any peephole optimizer can do this for you... ;-)
> >
> > Skip
>
> ..that looks a lot faster than the 'normal' way!
> (maybe there should be a built-in operator or something to do exactly that)
>
Firstly, thanks to everyone for their replies to this "trivial" question!
This is why I think it might be an idea to have a "swap" function in Python.
Besides making for more readable code - ok ok, I have not done a survey and
have no statistical or theoretical data to support this claim but let's
pretend humans understand words more readily than symbolic notation - a swap
function could generate the optimized code without any need for peephole
optimization.
Furthermore, as Aahz Maruch alludes to in another follow-up post, an "atomic"
swap might also be useful when writing multi-threaded applications.
That's my 2 Euro cents worth (which admittedly is not a lot!).
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