[Python-ideas] add a list.swap() method
Kristján Valur Jónsson
kristjan at ccpgames.com
Wed Jun 24 18:26:55 CEST 2009
I agree that as presented, it is a bit radical due to the concerns raised by Raymond and others.
But I still think it is a useful recipe for those that know what they are doing, and then perhaps as the standalone function, SwapLists(a,b) like we implemented in-house for our purposes.
I wonder, where do such recipes belong?
> -----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 Aahz
> Sent: 23. júní 2009 23:15
> To: python-ideas at python.org
> Subject: Re: [Python-ideas] add a list.swap() method
> 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
> > 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.
More information about the Python-ideas