[Python-ideas] add a list.swap() method
Kristján Valur Jónsson
kristjan at ccpgames.com
Thu Jun 25 11:05:41 CEST 2009
> -----Original Message-----
> From: python-ideas-bounces+kristjan=ccpgames.com at python.org
> [mailto:python-ideas-bounces+kristjan=ccpgames.com at python.org] On
> Behalf Of alex23
> Sent: 25. júní 2009 07:01
> To: python-ideas at python.org
> Subject: Re: [Python-ideas] add a list.swap() method
> Kristján Valur Jónsson <krist... at ccpgames.com> wrote:
> > The idea is to speed up the swapping of list elemenst so that
> > a.swap(b)
> > is equivalent to
> > a[:], b[:] = b[:], a[:]
> > but without all the overhead of creating slices, assigning them and
> so forth.
> I think I'd use a mapping to hold the lists so I could do a straight
> d['a'], d['b'] = d['b'], d['a']
This is roughly equivalent to
a, b = b, a
That's a completely different thing. Also, the whole point of this excersise is performance. This is why in our case, for example, we were using a list subclass (CRowset) in stead of wrapping the list, to shave off cpu cycles when accessing rows and columns in a rowset. A CRowset is essentially a list, with an extra attribute (header).
So, maybe I should rephrase the "idea."
The idea is for example to speed up the initialization of lists (and subclasses of lists) from other lists when the source list's contents is to be discarded. The semantics of
are the same as
a[:], b[:] = b[:], a[:]
a) is orders of magnitude faster
b) skips any special methods (such as __setslice__) and so may break class invariants of list subclasses, as pointed out on this list.
More information about the Python-ideas