Testing for reference equality in Python
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).
Thats the is
operator
print f1 is f2
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).