[Python-ideas] add a list.swap() method

Kristján Valur Jónsson kristjan at ccpgames.com
Tue Jun 23 22:23:31 CEST 2009


Hello there.
Please see my feature request: http://bugs.python.org/issue6326

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.
In particular, it can help reduce the cost of creating instances of list subclasses, from raw lists:

class mylist(list):
    def first(self):
                 return self[0]

m = mylist(source_list)

This certainly creates m, but does so by copying source_list.  If all we want to do is turn source_list into a mylist instance, it is much quicker to:
m = mylist()
m.swap(source_list)

See the above issue for initial comments, especially concerns on how this can bypass all kind of insertion semantics of list subclasses.

Cheers,

Kristján
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20090623/ac5b5885/attachment.html>


More information about the Python-ideas mailing list