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

Aahz aahz at pythoncraft.com
Wed Jun 24 01:15:21 CEST 2009


On Tue, Jun 23, 2009, Raymond Hettinger wrote:
>
> [Kristj?n Valur J?nsson]
>>
>> 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.
>
> The problem with the OP's example is that it only makes sense in
> interactions between a list and a list subclass that won't be broken
> by it.  For straight list-to-list interactions, it is better to write
> "a,b = b,a" (though this is not exactly the same thing since the
> identity of a and b will change, not just their contents).  For list
> subclass uses (a more advanced topic), some example will work and some
> won't (I gave two failing examples in the tracker discussion).  When
> the list subclass is a C extension written by a third-party, it may
> not be possible to know whether or not a swap will break invariants.
> That's a killer.

Thanks for the detailed response and explanation of why Kristjan wants
the feature and why it isn't the same thing as the standard tuple swap.
Your assessment makes sense, and I agree that this doesn't belong as a
standard list feature.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"as long as we like the same operating system, things are cool." --piranha



More information about the Python-ideas mailing list