有没有办法从指向它的弱代理中获取原始对象?例如,有相反的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)]
我需要从中获取唯一other成员列表my_list。我喜欢这样的任务的方式是使用set:
unique_others = {x.other for x in my_list}
不幸的是,这抛出TypeError: unhashable type: 'weakproxy'
unique_others = []
for x in my_list:
if x.other in unique_others:
continue
unique_others.append(x.other)
但标题中提到的一般问题仍然存在。如果我只能my_list控制并且others被埋在某个库中并且有人可能随时删除它们,我想通过在列表中收集非弱引用来防止删除怎么办?
repr()对象本身的,而不是<weakproxy at xx to Other at xx>
我想应该有一些weakref.unproxy我不知道的事情。