Basically:
If I declare a bytearray somewhere:
arr = bytearray(somestr)
Then create a memoryview of it:
view = memoryview(arr)
Can I be sure that for as long as I have a reference to the view object somewhere, that the bytearray will remain?
i.e:
def foo():
arr = bytearray("hello world")
return memoryview(arr)
view = foo()
Will garbage collection ever remove the original bytearray? Or does this count as a reference?
It counts as a reference. However you can call release()
on the view to remove that reference:
>>> class A(bytes):
... def __del__(self):print('called')
...
>>> a =A()
>>> m = memoryview(a)
>>> del a
>>> m
<memory at 0x7fddcb00a288>
>>> len(m)
0
>>> m.release()
called
Note that you can access the underlying object from the view using the obj
attribute.
In generaly, anything that is not described explicitly as a weak reference holds an actual reference. In memory-managed languages that's the default.