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:
class Obj:
def __init__(self, value):
self.value = value
print Obj(1234) == Obj(1234) # False
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:
class Obj:
def __init__(self, value):
self.value = value
def __eq__(self, other):
return self.value == other.value
print Obj(1234) == Obj(1234) # True
To fix this, you can "monkey-patch" the class:
class Obj:
def __init__(self, value):
self.value = value
print Obj(1234) == Obj(1234) # False
Obj.__eq__ = lambda a, b: a.value == b.value
print Obj(1234) == Obj(1234) # True
Or compare them by their values directly.
print Obj(1234).value == Obj(1234).value
Compare the values when possible because monkey-patching may break seemingly unrelated code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With