In Python, is object() equal to anything besides itself?
python
Solution
`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.
Problem
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.