46

Say I have a class in Python that has an eq method defined for comparing attributes for equality:

class Foo(object):
    # init code...

    def __eq__(self, other):
        # usual eq code here....

How can I then compare two instances of Foo for reference equality (that is test if they are the same instance)? If I do:

f1 = Foo()
f2 = Foo()
print f1 == f2

I get True even though they are different objects.

Adam Parkin
  • 17,891
  • 17
  • 66
  • 87
  • 1
    Nevermind, figured it out, the "is" operator: f1 = Foo() f2 = Foo() print f1 is f2 returns false. Reference: http://www.tutorialspoint.com/python/python_basic_operators.htm – Adam Parkin Sep 01 '11 at 17:30
  • Nice work finding the answer yourself. Even though you solved the problem yourself, it's still apropriate to place the solution as a regular answer, not a comment to the question. – SingleNegationElimination Sep 01 '11 at 17:32
  • `is` is correct, but you can also use `id(f1) == id(f2)`. – agf Sep 01 '11 at 17:32
  • Which I tried to do, but don't have the rep yet for doing so. – Adam Parkin Sep 01 '11 at 17:32
  • @agf: nice idea, but that sounds suspicously like an answer to the question. Consider adding it as such. – SingleNegationElimination Sep 01 '11 at 17:33
  • @Adam Parkin: You should be able to [answer your own question Regardless of reputation](http://meta.stackexchange.com/questions/6593/limiting-self-answers), although you will see a pop-up advising you that you should only provide an answer if it really is an answer to the question. – SingleNegationElimination Sep 01 '11 at 17:36
  • @Token I don't like to post answers showing the wrong way to do something :) – agf Sep 01 '11 at 18:11
  • possible duplicate of [How do I check if two variables reference the same object in Python?](http://stackoverflow.com/questions/3647546/how-do-i-check-if-two-variables-reference-the-same-object-in-python) – Ciro Santilli OurBigBook.com Apr 17 '15 at 18:27

3 Answers3

81

Thats the is operator

print f1 is f2
SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
8

f1 is f2 checks if two references are to the same object. Under the hood, this compares the results of id(f1) == id(f2) using the id builtin function, which returns a integer that's guaranteed unique to the object (but only within the object's lifetime).

Under CPython, this integer happens to be the address of the object in memory, though the docs mention you should pretend you don't know that (since other implementation may have other methods of generating the id).

Eli Collins
  • 8,375
  • 2
  • 34
  • 38
6

Use the is keyword.

print f1 is f2

Some interesting things (that are implementation dependent I believe, but they are true in CPython) with the is keyword is that None, True, and False are all singleton instances. So True is True will return True.

Strings are also interned in CPython, so 'hello world' is 'hello world' will return True (you should not rely on this in normal code).

Jonathan Sternberg
  • 6,421
  • 7
  • 39
  • 58
  • 2
    Just to note another wart of the CPython implementation: `id`s of ints are *not* always the same... `x=123123; ...some code...; x is 123123` will sometimes fail. This is because int "objects" are dynamically allocated, so sometimes duplicates are created. To my knowledge, only ints and strings are dynamically created this way, so they're the only time one has to worry about `is` behaving like this. – Eli Collins Sep 01 '11 at 17:38