Comparison of functions
steve at REMOVETHIScyber.com.au
Sat Jul 30 20:18:02 CEST 2005
On Sat, 30 Jul 2005 16:43:00 +0000, Adriano Varoli Piazza wrote:
> If you want to treat numbers as strings, why not convert them before
> sorting them?
Because that changes the object and throws away information.
Here is a list, containing one complex number converted to a string, and
one string that just happens to look like a complex number:
Now convert it back the way it was.
> Python is just saying "He's trying to sort complex
> numbers. No can do".
Python is quite happy to sort a list with one single complex number and
nothing else, so it is not SORTING complex numbers that Python objects
to, merely greater or less than comparisons. It is an accident of
implementation that Python blindly uses GT or LT comparisons for sorting
complex numbers, but not other objects.
> You're trying to make it guess that you want them
> sorted as strings, not as numbers. I don't see how Python treats things
> the same way. I see that real numbers and strings can be compared and
> sorted (asciibetically? don't remember). It has nothing to do with
> complex numbers. The abstraction or overloading or what it is fails,
> because they don't have an order as numbers, and Py is not intelligent
> enough to know that you want them asciibetized
Which has been my point all along: Python confuses the specific case of
NUMERIC COMPARISONS with the general case of SORTING. Worse, Python
doesn't even do that consistently: it is quite happy to let you compare
floats with strings, even though mathematically that is just as
much nonsense as to compare floats with complex numbers.
As I've said, the two are similar enough that such a mistake is easy to
make. And at this time, I'm not sure how to implement a better solution,
or even if there is a better solution, but I am giving it some thought.
More information about the Python-list