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

Terry Reedy tjreedy at udel.edu
Thu Jun 25 19:54:02 CEST 2009

Ron Adam wrote:
> Nick Coghlan wrote:
>> Kristján Valur Jónsson wrote:
>>> 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
>>> a.swap(b)
>>> are the same as
>>> a[:], b[:] = b[:], a[:]
>>> but it 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.

> It sounds to me like what is really desired in this case is not a 
> copy/swap operation but a type mutate operation on the data.  So instead 
> of copying the data, the data's type is mutated to another type 
> providing that the data format is compatible.

'Compatible' is the keyword. As I understand him, Kristján's problem is 
initializating list subclasses with additional fields, which are *not* 

Class mutation is already possible for user-defined classes.

More information about the Python-ideas mailing list