I can’t figure this out. I have two dictionaries which are identical. I use a standard method to determine the differences, of which there should be none. But certain value types are always returned as differences, even when they are not. For example, if a value is a pymongo.bson.ObjectId, the method fails to evaluate it as the same.
d1 = {'Name':'foo','ref1':ObjectId('502e232ca7919d27990001e4')}
d2 = {'Name':'foo','ref1':ObjectId('502e232ca7919d27990001e4')}
d1 == d2
returns:
True
But:
set((k,d1[k]) for k in set(d1) & set(d2) if d1[k] != d2[k])
returns:
set([('ref1',Objectid('502e232ca7919d27990001e4'))])
So I’ve figured out that this is weird, no?
d1['ref1'] == d2['ref1'] # True
d1['ref1'] != d2['ref1'] # False
What the?????!?!??!!?
ObjectId('502e232ca7919d27990001e4')creates a new object and by default!=compares references. Try for example:This will evaluate to false, because they are difference instances, even if they hold the same value. To make this work, the class must implement the eq method:
To fix this, you can “monkey-patch” the class:
Or compare them by their values directly.
Compare the values when possible because monkey-patching may break seemingly unrelated code.