Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4abd5f0

Browse files
author
Skip Montanaro
committed
Allow list sort's comparison function to explicitly be None. See SF patch
661092.
1 parent fe8496c commit 4abd5f0

4 files changed

Lines changed: 34 additions & 6 deletions

File tree

Doc/lib/libstdtypes.tex

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ \subsubsection{Mutable Sequence Types \label{typesseq-mutable}}
925925
{same as \code{del \var{s}[\var{s}.index(\var{x})]}}{(3)}
926926
\lineiii{\var{s}.reverse()}
927927
{reverses the items of \var{s} in place}{(6)}
928-
\lineiii{\var{s}.sort(\optional{\var{cmpfunc}})}
928+
\lineiii{\var{s}.sort(\optional{\var{cmpfunc=None}})}
929929
{sort the items of \var{s} in place}{(6), (7), (8), (9)}
930930
\end{tableiii}
931931
\indexiv{operations on}{mutable}{sequence}{types}
@@ -970,10 +970,11 @@ \subsubsection{Mutable Sequence Types \label{typesseq-mutable}}
970970
the first argument is considered smaller than, equal to, or larger
971971
than the second argument. Note that this slows the sorting process
972972
down considerably; e.g. to sort a list in reverse order it is much
973-
faster to call method \method{sort()} followed by
974-
\method{reverse()} than to use method
975-
\method{sort()} with a comparison function that reverses the
976-
ordering of the elements.
973+
faster to call method \method{sort()} followed by \method{reverse()}
974+
than to use method \method{sort()} with a comparison function that
975+
reverses the ordering of the elements. Passing \constant{None} as the
976+
comparison function is semantically equivalent to calling
977+
\method{sort()} with no comparison function.
977978

978979
\item[(8)] Whether the \method{sort()} method is stable is not defined by
979980
the language (a sort is stable if it guarantees not to change the

Lib/test/test_sort.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,26 @@ def __lt__(self, other):
145145

146146
bug453523()
147147

148+
def cmpNone():
149+
global nerrors
150+
151+
if verbose:
152+
print "Testing None as a comparison function."
153+
154+
L = range(50)
155+
random.shuffle(L)
156+
try:
157+
L.sort(None)
158+
except TypeError:
159+
print " Passing None as cmpfunc failed."
160+
nerrors += 1
161+
else:
162+
if L != range(50):
163+
print " Passing None as cmpfunc failed."
164+
nerrors += 1
165+
166+
cmpNone()
167+
148168
if nerrors:
149169
print "Test failed", nerrors
150170
elif verbose:

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ What's New in Python 2.3 alpha 2?
1212
Core and builtins
1313
-----------------
1414

15+
- List objects' sort() method now accepts None as the comparison function.
16+
Passing None is semantically identical to calling sort() with no
17+
arguments.
18+
1519
Extension modules
1620
-----------------
1721

Objects/listobject.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,9 @@ listsort(PyListObject *self, PyObject *args)
16571657
if (!PyArg_UnpackTuple(args, "sort", 0, 1, &compare))
16581658
return NULL;
16591659
}
1660+
if (compare == Py_None)
1661+
compare = NULL;
1662+
16601663
merge_init(&ms, compare);
16611664

16621665
/* The list is temporarily made empty, so that mutations performed
@@ -2069,7 +2072,7 @@ PyDoc_STRVAR(count_doc,
20692072
PyDoc_STRVAR(reverse_doc,
20702073
"L.reverse() -- reverse *IN PLACE*");
20712074
PyDoc_STRVAR(sort_doc,
2072-
"L.sort([cmpfunc]) -- stable sort *IN PLACE*; cmpfunc(x, y) -> -1, 0, 1");
2075+
"L.sort(cmpfunc=None) -- stable sort *IN PLACE*; cmpfunc(x, y) -> -1, 0, 1");
20732076

20742077
static PyMethodDef list_methods[] = {
20752078
{"append", (PyCFunction)listappend, METH_O, append_doc},

0 commit comments

Comments
 (0)