[Python-ideas] __sort__ special member

Lefavor, Matthew (GSFC-582.0)[MICROTEL LLC] matthew.lefavor at nasa.gov
Fri Jul 20 20:55:34 CEST 2012


The sorted() function works on any object supporting an __iter__ method. It does not just sort "any object"; if the argument is not iterable, sorted() will raise a TypeError.

If you insist on talking about types, you could say it operates on any instance of the abstract class collections.Iterable. This includes generators. For example:

>>> import collections
>>> def gen():
...     yield 3
...     yield 2
...     yield 1
...
>>> isinstance(gen(), collections.Iterable)
True
>>> sorted(gen())
[1, 2, 3]

As for your other point, users can already support whatever ordering they wish using the "key" argument to the sorted() function. For example, lexicographic ordering is already supported by using str.lower as the key function. For example:

>>> test_list = ["python 3", "PYTHON 4", "Python 2", "pYtHoN 1"]
>>> sorted(test_list)
['PYTHON 4', 'Python 2', 'pYtHoN 1', 'python 3']
>>> sorted(test_list, key=str.lower)
['pYtHoN 1', 'Python 2', 'python 3', 'PYTHON 4']

If you have an object for which you wanted to specify your own default comparison behavior, you could do so by specifying your own implementations of __lt__ or __eq__.

Matthew Lefavor


From: Mark Adam <dreamingforward at gmail.com<mailto:dreamingforward at gmail.com>>
Date: Friday, July 20, 2012 2:29 PM
To: "python-ideas at python.org<mailto:python-ideas at python.org>" <python-ideas at python.org<mailto:python-ideas at python.org>>
Subject: Re: [Python-ideas] __sort__ special member

On Fri, Jul 20, 2012 at 11:44 AM, Mark Adam <dreamingforward at gmail.com<mailto:dreamingforward at gmail.com>> wrote:
On Fri, Jul 20, 2012 at 11:23 AM, Oleg Broytman <phd at phdru.name<mailto:phd at phdru.name>> wrote:
On Fri, Jul 20, 2012 at 06:11:52PM +0200, David Townshend <aquavitae69 at gmail.com<mailto:aquavitae69 at gmail.com>> wrote:
> I was quite surprised to find that nobody seems to have suggested this
> before, because it seems like an obvious idea. Basically, add a special
> method __sort__ which, if specified, is used when sorted() is called. I can
> think of two immediate use cases:
>
> 1. When an object wants sorted() to return something other than a list,
> e.g. dict.__sort__ could return an OrderedDict.
> 2. When there is a more efficient method of sorting a specific sequence.
> E.g. sorting a range object should be trivial.
>
> Is there some obvious reason why nobody has suggested this before?  Is it
> worth pursuing?

   Because it's too application-specific?

What are you talking about.  This would solve the issue of ordering for dicts that became contentious a few years ago.  If people don't like the arbitrary ordering, they can subclass it.

Sorry, I'll tone down my words.  But this makes more sense than sort() as a built-in.  In theory, sort is for collections, not just any object.  So it could belong to an abstract collections base type.   This would allow users to select lexical orderings, for example, rather than ASCII.  It seems strange that no one thought of it before(it that true?).

mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120720/7bb06210/attachment.html>


More information about the Python-ideas mailing list