1

Why does the following piece behave like it behaves?

>>> '10' > 100
True
>>> 100 < '10'
True

Shouldn't it raise an exception?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Sergey
  • 47,222
  • 25
  • 87
  • 129

5 Answers5

6

From the documentation:

CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.

So it's just something that happens in CPython ('int' < 'str'), but that isn't guaranteed to happen in other implementations.

In fact, this behaviour has been removed in python3:

>>> '10' > 100
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() > int()
>>> 100 < '10'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < str()
jcollado
  • 39,419
  • 8
  • 102
  • 133
  • Actually, it's supposed to work on Jython, Pypy, IronPython, etc. It's part of the language definition. – tkone Jan 04 '12 at 18:55
1

From manual:

CPython implementation detail: Objects of different types except numbers are 
ordered  by their type names; objects of the same types that don’t 
support proper comparison are ordered by their address.

So if you compare this two types: int / string you have a lexicographic order bye the type of elements

DonCallisto
  • 29,419
  • 9
  • 72
  • 100
0

Because Python implements implicit type conversions for numbers so they may be printed as strings without doing an explicit conversion.

Python is converting 1000 into the string "1000" when doing the comparison to the string "10". And according to the Python intepreter, "1000" is indeed larger than "10".

This is why: "I've got %s bananas" % 5000 works, and unlike in C or another language without implicit type conversion, I didn't have to do printf("I've got %i bananas", 5000);

Check out Python docs chapter 5: Built-in Types

gahooa
  • 131,293
  • 12
  • 98
  • 101
tkone
  • 22,092
  • 5
  • 54
  • 78
  • I cannot see how this is answering the question asked, although it is helpful nonetheless. – gahooa Jan 04 '12 at 18:55
  • @gahooa Actually, it's fully relevant. Python is converting 1000 into the string "1000" when doing the comparison to the string "10". And according to the Python intepreter, '1000' is indeed larger than "10". `>>> if "1000" > "10": print True ... True` – tkone Jan 04 '12 at 18:58
  • I see. Perhaps a simple edit to the answer would clarify. Thanks! – gahooa Jan 05 '12 at 16:14
0

different operators are getting called, at one point int's __gt__, at another, str's __lt__

check this:

class t(int):
  def __gt__(self, other):
    print 'here', v
    return v

class t2(str):
  def __lt__(self, other):
    v = super(t2, self).__lt__(other)
    print 'ohere', v
    return v

if __name__ == '__main__':
  a = t('10')
  b = t2(100)

  a > b
  b < a
muckabout
  • 1,923
  • 1
  • 19
  • 31
-1

I am not 100% sure, but some internal type conversion might be happening here. It might be doing what is called lexicographic comparison, where '1' which is 49 in ASCII is greater than 1 (the first digit), and so on.

Ambidextrous
  • 810
  • 6
  • 14