Annoying behaviour of the != operator

Steven D'Aprano steve at REMOVETHIScyber.com.au
Fri Jun 10 16:57:19 CEST 2005


On Thu, 09 Jun 2005 08:10:09 -0400, Dan Sommers wrote:

>>> The main problem is that Python is trying to stick at least three
>>> different concepts onto the same set of operators: equivalence (are
>>> these two objects the same?), ordering (in a sorted list, which comes
>>> first?), and mathematical "size".
[snip]
>>> This gives the wacky world where
>>> "[(1,2), (3,4)].sort()" works, whereas "[1+2j, 3+4j].sort()" doesn't.
> 
> Python inherits that wackiness directly from (often wacky) world of
> Mathematics.
> 
> IMO, the true wackiness is that
> 
>     [ AssertionError, (vars, unicode), __name__, apply ].sort( )
> 
> "works," too.  Python refusing to sort my list of complex numbers is a
> Good Thing.

Only if you understand sorting as being related to the mathematical
sense of size, rather than the sense of ordering. The two are not the
same!

If you were to ask, "which is bigger, 1+2j or 3+4j?" then you
are asking a question about mathematical size. There is no unique answer
(although taking the absolute value must surely come close) and the
expression 1+2j > 3+4j is undefined.

But if you ask "which should come first in a list, 1+2j or 3+4j?" then you
are asking about a completely different thing. The usual way of sorting
arbitrary chunks of data within a list is by dictionary order, and in
dictionary order 1+2j comes before 3+4j because 1 comes before 3.

This suggests that perhaps sort needs a keyword argument "style", one of
"dictionary", "numeric" or "datetime", which would modify how sorting
would compare keys.

Perhaps in Python 3.0.


-- 
Steven.






More information about the Python-list mailing list