I think this is expected behaviour but want to check and maybe find out why, as the research I have done has come up blank
I have a function that pulls data, creates a new instance of my custom class, then appends it to a list. The class just contains variables.
I then pickle that list to a file using protocol 2 as binary, later I re-run the script, re-pull the data from my source, I have a new list with my custom class instances, for testing I keep the data the source data the same.
Reload the pickle file
Now when I do a:
print source_list == pickle_list
this always comes back False
, and I have no idea why, if I print the lists or look at the structure they look exactly the same.
Any ideas would be brill, this is my last little bit I need to sort.
Comparing two objects of the same class yields False by default (unless they are the same single object), even if they have the same contents; in other words, two intuitively "identical" objects from the same class are considered different, by default. Here is an example:
>>> class C(object):
... def __init__(self, value):
... self.value = value
...
>>>
>>> C(12) == C(12)
False
You want to define __eq__()
(and __ne__()
) in your custom class so that it yields True for objects that contain the same data (respectively False). More information can be found in the official documentation. For the above example, this would be:
>>> class C(object):
... # ...
... def __eq__(self, other):
... return self.value == other.value
... def __ne__(self, other):
... return not self == other # More general than self.value != other.value
...
>>> C(12) == C(12) # __eq__() is called
True
>>> C(12) != C(12) # __ne__() is called
False