Python does not consider equivalent objects to be equivalent

class, logic, object, python

Solution

The default equality comparison in Python is to check for identity (i.e. two objects are the same object).

According to the Python Library Reference:

Non-identical instances of a class normally compare as non-equal unless the class defines the __eq__() method or the __cmp__() method.

To create your own definition of equivalence, you need to define an __eq__ method. Here is one way to do it:

class fubar(object):

    def __eq__(self, other):
        'Fubar objects are considered equal if they have the same contents'
        if type(self) != type(other):
            return NotImplemented
        return vars(self) == vars(other)

The return value of NotImplemented signals that fubar doesn't know how to make the comparison and is giving the other object a chance to do the comparison.

The Python Language Reference has this to say about NotImplemented:

This type has a single value. There is a single object with this value. This object is accessed through the built-in name NotImplemented. Numeric methods and rich comparison methods may return this value if they do not implement the operation for the operands provided. (The interpreter will then try the reflected operation, or some other fallback, depending on the operator.) Its truth value is true.

Problem

I am pickling, compressing, and saving python objects. I want to be able to double-check that that the object I saved is the exact same object that is returned after decompression and depickling. I thought there was an error in my code, but when I boiled the problem down to a reproducible example I found that python does not consider two seemingly identical objects created at two different points in time to be equal. Here is a reproducible example: ``` class fubar(object): pass print(fubar() == fubar()) #False ``` Why does python consider these two objects to be not equal and what is the most pythonic way to check that two objects are indeed identical?

Original source

Related problems