In a standard dictionary, the following code is allowed
>>> d
{1: {2: 3}}
>>> d[1][3] = d[1]
>>> d
{1: {2: 3, 3: {...}}}
Since any function that relies on _dfs_generator counts on the dict ending at some finite depth, this scenario needs to be taken care of in some way.
Easy but limiting
_dfs_generator has a default max_depth, above which it stops working and possibly throws an exception.
Harder but better
__setitem__ enforces that the variable to the right of the assignment expression does not contain the variable on the left side, thus assuring that no loops occur in the NestedDict.
In a standard dictionary, the following code is allowed
Since any function that relies on
_dfs_generator
counts on the dict ending at some finite depth, this scenario needs to be taken care of in some way.Easy but limiting
_dfs_generator
has a defaultmax_depth
, above which it stops working and possibly throws an exception.Harder but better
__setitem__
enforces that the variable to the right of the assignment expression does not contain the variable on the left side, thus assuring that no loops occur in theNestedDict
.