[New-bugs-announce] [issue24075] list.sort() should do quick exit if len(list) <= 1

Sergey B Kirpichev report at bugs.python.org
Wed Apr 29 15:30:43 CEST 2015


New submission from Sergey B Kirpichev:

If there is nothing to sort (i.e. one item), why call key function at all?

In my practical situation, simplest key() function will lead to recursion in case of such trivial lists.  I can make similar cmp-type function (i.e. mycmp=lambda a, b: cmp(key(a), key(b))) and then wrap this with cmp_to_key.  But that looks silly.

Simple test case:
$ cat a.py
a = [1]

def spam(x):
    raise Exception

a.sort(key=spam)
print(a)
$ python a.py 
Traceback (most recent call last):
  File "a.py", line 6, in <module>
    a.sort(key=spam)
  File "a.py", line 4, in spam
    raise Exception
Exception

----------
components: Interpreter Core
files: trivial-sorting-py3.patch
keywords: patch
messages: 242222
nosy: Sergey.Kirpichev
priority: normal
severity: normal
status: open
title: list.sort() should do quick exit if len(list) <= 1
type: behavior
versions: Python 2.7, Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 3.6
Added file: http://bugs.python.org/file39231/trivial-sorting-py3.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24075>
_______________________________________


More information about the New-bugs-announce mailing list