Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is `object() > 0` True in Python?

Tags:

python

In [32]: object() > 0
Out[32]: True

In fact, it's greater than any integer I've tried.

like image 614
satoru Avatar asked Jul 19 '12 04:07

satoru


1 Answers

Because Python 2.x tried to make available comparison between objects of different types (even if they do not make sense).

It was fixed on Python 3:

>>> object() > 0
Traceback (most recent call last):
  File "<pyshell#320>", line 1, in <module>
    object() > 0
TypeError: unorderable types: object() > int()

The order used in Python 2.x:

None < Numbers < [Other types in alphabetical order]

Numbers are: int, bool and float but not complex because they can't be compared

Also, objects from old style classes (named instance objects) stay between None and Numbers for some odd reason.

like image 134
JBernardo Avatar answered Sep 25 '22 23:09

JBernardo