5

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?

Droonkid
  • 55
  • 14
  • 1
    Can you show us what your function...or rather the class looks like? – taesu Feb 10 '15 at 03:06
  • Your Fraction implementation would be good to see, or the relevant parts with the overloads. – Marcin Feb 10 '15 at 03:06
  • What about your `__init__` method? Try to show us the complete minimal code. – John Zwinck Feb 10 '15 at 03:08
  • What probably happens is that you either have the comparable mixin, or it just compares the strings from __repr__. – Michel Müller Feb 10 '15 at 03:10
  • @Michel Müller what is the comparable mixin? – Droonkid Feb 10 '15 at 03:12
  • Just to say, I am a self-taught pythonist and have a bunch of complex things I have mastered and a bunch of simple things I don't know exist. – Droonkid Feb 10 '15 at 03:17
  • 1
    I've noticed that the same behavior is for this simple case as well: http://pastebin.com/EX7vpvm9. It only works like this in 2.7. In 3.4 it wont compile even. – Marcin Feb 10 '15 at 03:31
  • @Droonkid I'd recommend shortening your question to something like Marcin's example to get more attention, and add a python2 tag – Ryan Haining Feb 10 '15 at 03:52
  • @Droonkid I thought there was something standard, I was wrong. However, you can define something yourself like [this](http://stackoverflow.com/questions/6907323/comparable-classes-in-python-3), which simplifies the implementation of comparisons quite a bit. – Michel Müller Feb 10 '15 at 05:25
  • Ohh, i know about the comparison methods, I just haven't implemented them yet and I noticed this – Droonkid Feb 11 '15 at 00:59

1 Answers1

3

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)
taesu
  • 4,482
  • 4
  • 23
  • 41
  • Could you explain more. I dont understand why if I run `Foo(5) > Foo(2)` it will give False, and then when I run the same test again `Foo(5) > Foo(2)` it will give True? – Marcin Feb 10 '15 at 03:30
  • I know but that the id is different, but why is the comparison different each time? – Droonkid Feb 10 '15 at 03:39
  • When you do `Foo(1)`, this instantiate a class of Foo, thus it can vary every time. However, if you do `a = Foo(1)`, and use `a` to compare with `b` in the same manner, you will always get the same result, b/c nothing has been changed, and you are referring to the same variable with same id – taesu Feb 10 '15 at 03:41
  • 1
    It seems like you're right, but I can't find anything in the python reference to support this. The closest I see is a note sying *Most other objects of built-in types compare unequal unless they are the same object; the choice whether one object is considered smaller or larger than another one is made arbitrarily but consistently within one execution of a program.* [here](https://docs.python.org/2/reference/expressions.html#not-in) but that calls out built-ins specifically and is in the python3 docs too (and this doesn't work in python3) – Ryan Haining Feb 10 '15 at 04:01
  • Ahhh, I understand now, so it compares the id's of the instances instead of their values – Droonkid Feb 11 '15 at 00:57
  • glade you did! I was curious myself. – taesu Feb 11 '15 at 01:05