Buttons840
Buttons840

Reputation: 9637

In Python, is object() equal to anything besides itself?

If I have the code my_object = object() in Python, will my_object be equal to anything except for itself?

I suspect the answer lies in the __eq__ method of the default object returned by object(). What is the implementation of __eq__ for this default object?

EDIT: I'm using Python 2.7, but am also interested in Python 3 answers. Please clarify whether your answer applies to Python 2, 3, or both.

Upvotes: 20

Views: 3075

Answers (2)

roippi
roippi

Reputation: 25974

object().__eq__ returns the NotImplemented singleton:

print(object().__eq__(3))
NotImplemented

By the reflexive rules of rich comparisons, when NotImplemented is returned, the "reflected" operation is tried. So if you have an object on the RHS that returns True for that comparison, then you can get a True response even though the LHS did not implement the comparison.

class EqualToEverything(object):
    def __eq__(self,other):
        return True

ete = EqualToEverything()

ete == object() # we implemented `ete.__eq__`, so this is obviously True
Out[74]: True

object() == ete # still True due to the reflexive rules of rich comparisons
Out[75]: True

python 2 specific bit: if neither object implements __eq__, then python moves on to check if either implement __cmp__. Equivalent reflexive rules apply here.

class ComparableToEverything(object):
    def __cmp__(self,other):
        return 0

cte = ComparableToEverything()

cte == object()
Out[5]: True

object() == cte
Out[6]: True

__cmp__ is gone in python 3.

In both python 2 and 3, when we exhaust all of these comparison operators and all are NotImplemented, the final fallback is checking identity. (a is b)

Upvotes: 28

jonrsharpe
jonrsharpe

Reputation: 122089

object doesn't implement __eq__, so falls back on the default comparison id(x) == id(y), i.e. are they the same object instance (x is y)?

As a new instance is created every time you call object(), my_object will never* compare equal to anything except itself.

This applies to both 2.x and 3.x:

# 3.4.0
>>> object().__eq__(object())
NotImplemented

# 2.7.6
>>> object().__eq__(object())

Traceback (most recent call last):
  File "<pyshell#60>", line 1, in <module>
    object().__eq__(object())
AttributeError: 'object' object has no attribute '__eq__'

* or rather, as roippi's answer points out, hardly ever, assuming sensible __eq__ implementations elsewhere.

Upvotes: 19

Related Questions