Jonas Kaufmann Jonas Kaufmann - 4 years ago 173
Python Question

Does == follow from id(...) == id(...)?

If I have two objects o1 and o2, and we know that

id(o1) == id(o2)


returns true.

Then, does it follow that

o1 == o2


Or is this not always the case? The paper I'm working on says this is not the case, but in my opinion it should be true!

Answer Source

The paper is right. Consider the following.

class WeirdEquals:
    def __eq__(self, other):
        return False

w = WeirdEquals()
print("id(w) == id(w)", id(w) == id(w))
print("w == w", w == w)

Output is this:

id(w) == id(w) True
w == w False
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download