Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two python objects both greater than or less than each other

I am writing a Fractions class and while messing around I noticed this:

>>> class Test:
    def __init__(self):
        pass


>>> Test()>Test()
True
>>> Test()>Test()
False

Why is this?

like image 839
Droonkid Avatar asked Oct 19 '22 17:10

Droonkid


1 Answers

Put simply, your comparisons aren't directly on the data of the class, but the instance of class itself (id(Foo(1))), because you have not written it's comparisons explicitly.

It compares the id of the instances, thus sometimes it's True and other times it's False.

 Foo(1)
=> <__main__.Foo instance at 0x2a5684>
   Foo(1)
=> <__main__.Foo instance at 0x2a571c>
   Foo(1)
like image 73
taesu Avatar answered Nov 04 '22 19:11

taesu