1

I've tried to use the following class to make a "list" of nodes which could contain nested nodes of the same type as dict items:

$ ipython
Python 3.5.3 (default, Aug 19 2017, 03:39:05) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: class value_dict_pair():
...: 
...:     def __init__(self, value=None, data={}):
...:         self.data = data
...:         self.value = value
...: 

Make a default initialized instance: w/ value as None and empty dict in data:

In [2]: p=value_dict_pair()

In [3]: p.data
Out[3]: {}

Ok, it is really empty. Going to add another default initialized instance of value_dict_pair class to the some key of the first "node":

In [4]: p.data['some']=value_dict_pair()

In [5]: p.data
Out[5]: {'some': <__main__.value_dict_pair at 0x7f5c030b2c18>}

In [6]: p.data['some']
Out[6]: <__main__.value_dict_pair at 0x7f5c030b2c18>

Ok, now p.data has the only key some initialized with value_dict_pair instance. But,

In [7]: p.data['some'].data
Out[7]: {'some': <__main__.value_dict_pair at 0x7f5c030b2c18>}

why the "nested" instance also has the some key?? (yeah, looking to the 0x7f5c030b2c18 address that is the same instance)

In [8]: p.data['some'].data['some']
Out[8]: <__main__.value_dict_pair at 0x7f5c030b2c18>

In [9]: p.data['some'].data['some'].data
Out[9]: {'some': <__main__.value_dict_pair at 0x7f5c030b2c18>}

In [10]: p.data['some'].data['some'].data['some']
Out[10]: <__main__.value_dict_pair at 0x7f5c030b2c18>

... and so on.

What I didn't get is how adding another (supposed) default initialized value_dict_pair instance could lead to that (cycle referenced dict)???

zaufi
  • 6,811
  • 26
  • 34

0 Answers0