Box icon indicating copy to clipboard operation
Box copied to clipboard

Fixing #251 support for circular references in lists

Open CNSeniorious000 opened this issue 1 year ago • 0 comments

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.

CNSeniorious000 avatar Sep 18 '23 06:09 CNSeniorious000