それを指すweakproxyから元のオブジェクトを取得する方法はありますか? たとえば、の逆はあり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私が気づいていない
ようなものがあるはずだと思います。