cdgriffith / Box

Python dictionaries with advanced dot notation access
https://github.com/cdgriffith/Box/wiki
MIT License
2.61k stars 106 forks source link

Fixing #251 support for circular references in lists #259

Closed CNSeniorious000 closed 4 months ago

CNSeniorious000 commented 1 year ago

I read #251 and think that using is operator to identify objects may fix the problem id brings.

For now, you can:

  1. initialize a BoxList with a list with circular references
>>> from box import BoxList
>>> a = []
>>> a.append(a)
>>> a
[[...]]
>>> b = BoxList(a)
>>> b
BoxList([[...]])
>>> b[0][0][0][0] is b
True
  1. reference oneself after creation
>>> a = BoxList()
>>> a
BoxList([])
>>> a.append(a)
>>> a
BoxList([[...]])
>>> a[0][0][0][0] is a
True

Note this:

>>> a = []
>>> b = BoxList(a)
>>> b.append(a)
>>> b
BoxList([[]])
>>> b[0] is a
False

This is because:

  1. For performance, it is a waste to always hold a reference for the iterable passing to __init__.
  2. After initialization, a is not the same as b anymore, I think users appending b to a is not intended to append a to a.
cdgriffith commented 4 months ago

I have no idea how this fell off my radar, I apologize!

Thank you so much for looking into this!