osdir.com

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Python-Dev] python 3 niggle: None < 1 raises TypeError


On 18 February 2014 15:53, Terry Reedy <tjreedy at udel.edu> wrote:
> On 2/18/2014 2:35 AM, Greg Ewing wrote:
>
>>    results = sorted(invoices, key=attrgetter('duedate'), none='first')
>
> I think this is the best idea on the thread. As a pure enhancement, it could
> be added in 3.5. The only tricky part of the implementation is maintaining
> stability of the sort. The obvious implementation of swapping Nones with
> objects at one end would break that. Instead, a scan listing the positions
> of Nones should be followed by a series of block moves of objects (pointers)
> between the Nones. It would still be O(n).

This only works if the list entry is a simple None. If the None is
embedded in e.g. a tuple then it would fail:

>>> (1, 2, 3) < (1, 2, None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < NoneType()


Oscar