[Python-Dev] Cleaning-up the new unittest API

Terry Reedy tjreedy at udel.edu
Mon Nov 1 19:37:33 CET 2010


On 10/31/2010 10:55 PM, Michael Foord wrote:

> fact that sets / frozensets can't be sorted in the standard Python way
> (their less than comparison adheres to the set definition). This is
> something that will probably surprise many Python developers:

Any programmer who sorts (or uses functions that depend on proper 
sorting) should know and respect the difference between partial orders, 
such as set inclusion, and total orders, such as lex order of sequences. 
So I am surprised by the above claim ;-).

>  >>> a = [{2,4}, {1,2}]
>  >>> b = a[::-1]
>  >>> sorted(a)
> [set([2, 4]), set([1, 2])]
>  >>> sorted(b)
> [set([1, 2]), set([2, 4])]

The bug is not in the sort method, but the attempt to sort partially 
ordered items, which are not properly sortable.

a = [{2,4}, {1,2}]
b = a[::-1]
print(sorted(a,key=sorted))
#[{1, 2}, {2, 4}]
print(sorted(b,key=sorted))
#[{1, 2}, {2, 4}]

A test method (or internal branch) that depends on sorting to work 
properly could just refuse to work with sets (and frozensets).

-- 
Terry Jan Reedy



More information about the Python-Dev mailing list