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

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to