Python: Why does equality comparing an int with a string not throw an error?
python, types
Solution
This allows you, for example, to have a dictionary with keys of mixed types.
If you couldn't compare `1` and `"1"` for equality, you wouldn't be able to use them as keys into the same dictionary.
As things stand, you can compare them, and they always compare unequal:
The objects need not have the same type. If both are numbers, they are converted to a common type. Otherwise, objects of different types always compare unequal, and are ordered consistently but arbitrarily.
Problem
In Python 3 the attempt to order a string and an int (e.g. `1 > "1"`) throws a TypeError. Why does comparing a string with an int for equality not throw an error? (e.g. `1=="1"`) What is an example where comparing a string with an int makes sense? Why do JavaScript and SQL take a different approach? Related: How does Python compare string and int?