0

I have a dictionary where one of its values is an object. I am trying to serialise and de-serialise this dictionary using the Pickle module like so:

import pickle

class ExampleClass(object):
    def __init__(self, obj_id, name, type):
        self.obj_id = obj_id
        self.name = name
        self.type = type

my_obj = ExampleClass(1, 'tu42', 'bas5')

example_dict = {'object': my_obj,
                 'date': '02041980',
                 'event': 'test'}

with open("test.pickle", "wb") as handle:
    pickle.dump(example_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)

with open("test.pickle", "rb") as handle:
    reconstituted = pickle.load(handle)

print(example_dict)
print(reconstituted)
print(example_dict == reconstituted) # expected to be True

This gives the following output:

{'object': <__main__.ExampleClass object at 0x000001D726852148>, 'date': '02041980', 'event': 'test'}
{'object': <__main__.ExampleClass object at 0x000001D7268527C8>, 'date': '02041980', 'event': 'test'}
False

where the object value of the dict, 'object': my_obj is different and I do not understand why that is the case.

Any suggestions or information to make this example_dict pickle to be the same as its corresponding reconstituted value would be very useful.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Yannis
  • 1,682
  • 7
  • 27
  • 45
  • 1
    There are two facts. One is if you don't specify `equality`, the instances use the address to identify equality. Another is, a restored instance is another instance but not the original instance. Based on these two facts, it is reasonable that `example_dict == reconstituted` returns `False` as although these two instances contains the same content, they are still two different instances. – Sraw Aug 19 '19 at 09:53

1 Answers1

0

@Straw's comment pointed to ther right direction.

In case someone else might find themselves in a similar situtation, what is needed is to compare the object instances for equality based on their attributes as described here.

For the example presented in this story, implementing the __eq__ method will allow comparing the object instances:

import pickle
class ExampleClass(object):
    def __init__(self, obj_id, name, type):
        self.obj_id = obj_id
        self.name = name
        self.type = type

    def __eq__(self, other):
        if not isinstance(other, ExampleClass):
            return NotImplemented

        return self.obj_id == other.obj_id and self.name == other.name and self.type == other.type

my_obj = ExampleClass(1, 'tu42', 'bas5')

example_dict = {'object': my_obj,
                 'date': '02041980',
                 'event': 'test'}

with open("test.pickle", "wb") as handle:
    pickle.dump(example_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)

with open("test.pickle", "rb") as handle:
    reconstituted = pickle.load(handle)

print(example_dict)
print(reconstituted)
print(example_dict == reconstituted)

This will now return True.

Yannis
  • 1,682
  • 7
  • 27
  • 45