Recently, a colleague of mine tracked down a bug which was, in essence, caused by the fact that one side of a comparison in Python was a string, and the other was an integer, which led to behaviour that was sometimes expected and sometimes not.
1 2 3 4
This is one of the more unintuitive parts for Python for me. Intuitively, I would expect
some sort of a
TypeError if I am trying to compare things that are not of the same type.
But, apparently, in Python, when you compare two items of distinct types, Python compares
the types themselves.
Objects of different types, except different numeric types and different string types, never compare equal; such objects are ordered consistently but arbitrarily (so that sorting a heterogeneous array yields a consistent result). Furthermore, some types (for example, file objects) support only a degenerate notion of comparison where any two objects of that type are unequal. Again, such objects are ordered arbitrarily but consistently.
1 2 3 4
…whereas Ruby seems to behave fairly sanely, throwing an error – just like I expect. Perhaps it’s time to start using Ruby a bit more for my small scripts (though it often seems to lead to more time spent yak shaving than Python in the average case).
1 2 3 4 5
❧ Suggestions, comments, etc. can be emailed to