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

后端 未结 2 1886
天涯浪人
天涯浪人 2021-01-01 14:00

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

相关标签:
2条回答
  • 2021-01-01 14:37

    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)

    0 讨论(0)
  • 2021-01-01 14:48

    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.

    0 讨论(0)
提交回复
热议问题