Is there any way to get the original object from a weakproxy pointed to it? eg is there the inverse to weakref.proxy()
?
import weakref
class C(object):
def __init__(self, other):
self.other = weakref.proxy(other)
class Other(object):
pass
others = [Other() for i in xrange(3)]
my_list = [C(others[i % len(others)]) for i in xrange(10)]
I need to get the list of unique other
members from my_list
. The way I prefer for such tasks
is to use set
:
unique_others = {x.other for x in my_list}
Unfortunately this throws TypeError: unhashable type: 'weakproxy'
unique_others = []
for x in my_list:
if x.other in unique_others:
continue
unique_others.append(x.other)
but the general problem noted in the caption is still active.
What if I have only my_list
under control and others
are burried in some lib and someone may delete them at any time, and I want to prevent the deletion by collecting nonweak refs in a list?
repr()
of the object itself, not <weakproxy at xx to Other at xx>
I guess there should be something like weakref.unproxy
I'm not aware about.