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.