Python allows dictionaries to be compared with ==
import copy
child = {'name': 'child'}
parent_1 = {'name': 'parent', 'child': child}
parent_2 = copy.deepcopy(parent_1)
print(parent_1 == parent_2)
Prints True
, as you would expect it to.
Python also allows dictionaries to reference each other circularly.
child = {'name': 'child'}
parent_1 = {'name': 'parent', 'child': child}
child['parent'] = parent_1 # Create the circular reference
However, trying to use the ==
operator on dictionaries with circular references raises an error.
parent_2 = copy.deepcopy(parent_1)
print(parent_1 == parent_2)
Returns
C:\Python34\python.exe -i C:/Users/anon/.PyCharm40/config/scratches/scratch_5
Traceback (most recent call last):
File "C:/Users/anon/.PyCharm40/config/scratches/scratch_5", line 11, in <module>
print(parent_1 == parent_2)
RuntimeError: maximum recursion depth exceeded in comparison
How can I check two dictionaries with circular references for equality?