nickstanisha / nesteddict

A nested dictionary data structure for Python
MIT License
0 stars 0 forks source link

Outlaw self-referencing dictionaries #4

Open nickstanisha opened 7 years ago

nickstanisha commented 7 years ago

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.