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

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

This is actually more than any integer I've tried.

+5
source share
2 answers

Since Python 2.x tried to make comparisons between objects of different types available (even if they don't make sense).

This has been fixed in 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 is:

None < Numbers < [Other types in alphabetical order]

The numbers: int, booland float, but not complexbecause they can not be compared

In addition, objects from old-style classes (called instanceobjects) remain between Noneand Numbersfor some odd reason.

+12
source

It compares the address of the object. If you know C / C ++, this is pointer arithmetic.

>>> object() < object()
True
>>> object() > object()
False

() , , , "" .

, , .:)

+2

All Articles