13

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?

Community
  • 1
  • 1
e1i45
  • 1,519
  • 1
  • 14
  • 20
  • What do you mean "Why do JavaScript and SQL take a different approach?" `1 == '1'` yields `true` in javascript. – Marcin May 16 '12 at 11:47
  • @ Marcin: ´1=="1"´: Yes, JavaScript yields true. But Python yields false. – e1i45 May 16 '12 at 11:51
  • 1
    Your question is "why does it make sense to allow the comparison", so in that sense it is not a different approach. – Marcin May 16 '12 at 11:53

3 Answers3

7

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.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
7

The reason the orderings raise TypeError on non-comparable objects is to represent that there is no sensible answer, rather than any prediction on whether it would ever be useful. Allowing an equality test is sensible by this logic, insofaras there is an answer for "are two non-comparable objects equal?" (they aren't). See, eg, http://www.gossamer-threads.com/lists/python/dev/919516 .

lvc
  • 34,233
  • 10
  • 73
  • 98
  • The answer from @aix makes sense as well but it is unnecessarily complicated if the OP doesn't know about hash maps. This explanation is more straightforward imo. – jamylak May 16 '12 at 11:58
  • @jamylak Every programmer should know hashmaps, just like they should know about iteration or arithmetic. – Marcin May 16 '12 at 12:00
  • @Marcin Right but they over complicate the example. It is simply a matter of sensibility. – jamylak May 16 '12 at 12:01
1

Strength and weakness of languages typing

Typing of language could be strong or weak (loose). The stronger typing language has the less different types could be operated in the same operation. Weakness and strength of language typing don't have exact threshold - some language could have stronger typing then other and weaker than another one. Python typing is much stronger than JS.

== implemented as more of less weakly-typed operation. It can compare different types but you need to have both values of the same type to have a chance to obtain True. a == b #true means a and b is objects of the same type and have the equal values. > < in Python 3 implemented as strongly-typed operation and couldn't be performed on different types.

Community
  • 1
  • 1
I159
  • 29,741
  • 31
  • 97
  • 132
  • *you need to have both values of the same type to have a chance to obtain 'True'* — not always: `0 == 0.0 == 0j == Decimal(0) == Fraction(0, 1) == np.uint8(0) == False`, or for a container example `{1:2, 3:4}.keys() == {1, 3}`. – gerrit Jun 29 '20 at 15:34