[Patches] [ python-Patches-1491804 ] Simple slice support for list.sort() and .reverse()

SourceForge.net noreply at sourceforge.net
Fri Mar 9 04:07:51 CET 2007


Patches item #1491804, was opened at 2006-05-19 15:19
Message generated for change (Comment added) made by collinwinter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1491804&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Core (C code)
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Heiko Wundram (hwundram)
Assigned to: Nobody/Anonymous (nobody)
Summary: Simple slice support for list.sort() and .reverse()

Initial Comment:
As requested per

http://groups.google.de/group/comp.lang.python/browse_thread/thread/6feadf8170900e53/aa621eed0fe14050?hl=de#aa621eed0fe14050

list.sort() should support extra keyword arguments
start and stop, which specify a slice of the whole list
to sort inplace.

The attached patch implements this functionality, and
extends the sorted() builtin to also offer these
keyword arguments, and additionally implements slice
support (also with start, stop) for list.reverse().

The patch updates the list object methods and the
sorted builtin, and also updates the testsuite to check
for the new keyword arguments and updates the
documentation to list them.

----------------------------------------------------------------------

>Comment By: Collin Winter (collinwinter)
Date: 2007-03-08 22:07

Message:
Logged In: YES 
user_id=1344176
Originator: NO

I think anyone who was relying on that behavior was treading on
"implementation details" territory to begin with, so I wouldn't worry about
this particular semantics nit.

----------------------------------------------------------------------

Comment By: Heiko Wundram (hwundram)
Date: 2006-05-20 12:15

Message:
Logged In: YES 
user_id=791932

By the way, I've just become aware of the fact that this
patch changes the semantics of list.sort() somewhat, because
of an optimization I did to the code. The DSU-function isn't
called anymore when there are less than two items to sort,
i.e. the list or the slice to sort is one item long. This means:

---
def test(k):
    k.append(4)
    return k[0]
x = [[1,2,3]]
x.sort(key=test)
print x
---

will now print [[1,2,3]] (with the patch applied), whereas
Python 2.4 would've printed [[1,2,3,4]], but not have called
timsort either.

I don't know whether this breaks anything (or the old
behaviour was sensible); at least it should have to be
documented. I'd like feedback before I start either taking
out the optimization or documenting this.

----------------------------------------------------------------------

Comment By: Heiko Wundram (hwundram)
Date: 2006-05-20 07:18

Message:
Logged In: YES 
user_id=791932

The attached patch implements all of the old patch, and adds
the specified logic for array.reverse(), updates the
documentation for the array module and whatsnew25, and will
speed up the methods a slight little bit in the absense of
optimization when compiling Python.

----------------------------------------------------------------------

Comment By: Terry J. Reedy (tjreedy)
Date: 2006-05-20 01:14

Message:
Logged In: YES 
user_id=593130

Having thought about submitting an RFE for start/stop for 
reverse, I support the enhancement.  Please do the same 
for array.reverse(, so list and array.reverse continue to 
have the same signature.  Two uses: one way to swap 
partitions in place is reverse each partition and then the 
whole sequence; probably more useful is the slice reversal 
in one standard method for sequentially generating 
permutations in lexicographical order.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1491804&group_id=5470


More information about the Patches mailing list