-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
Description
EDIT: Hi!
>>> import weakref
>>> class A:
... def __init__(self, a):
... self.a = a
...
>>> a = A(A(A(A(A(None)))))
>>> aa = weakref.ref(a.a)
>>> aaa = weakref.proxy(a.a.a)
>>> aa_p = weakref.proxy(a.a)
>>> a.a
<__main__.A object at 0x7fcc503c0fc0>
>>> aa()
<__main__.A object at 0x7fcc503c0fc0>
>>> aa_p.__weakref__()
<__main__.A object at 0x7fcc503c0fc0>
>>> aaa.__weakref__()
Traceback (most recent call last):
File "<python-input-9>", line 1, in <module>
aaa.__weakref__()
~~~~~~~~~~~~~~~^^
TypeError: 'weakref.ProxyType' object is not callable
>>> del aa
>>> aa_p.__weakref__()
Traceback (most recent call last):
File "<python-input-11>", line 1, in <module>
aa_p.__weakref__()
~~~~~~~~~~~~~~~~^^
TypeError: 'weakref.ProxyType' object is not callable
Q: What are you trying to do?
A: Want to grab and store some hardref from a list[weakref.proxy]
, to keep refcount between 1 and 2; then use the proxy whose proxied obj refcount is 1 (missed a step: the ones that were 2 are now 1); delete the hardrefs "at the end" and let them go either 0 or 1
EDIT: Just promoting some weakref.proxy to become hardref, so I guess I can just replace the proxy with the actual object, making the list list[weakref.ProxyType | T]
EDIT: This means I still need to keep track of a weakref.ref for each weakref.proxy, to be able to get the actual object back
Q: why didn't you use weakref.ref instead?
A: sorry, first time using weakref by myself, my current code assumed no weakref at all, so using proxy seemed appealing due to "almost no change required"
EDIT: Thanks!