A more efficient way to use a weakref on an object as a property?
python, weak-references
Solution
I use weakref.proxy for this.
import weakref
class A(object):
def __init__(self, parent)
self.parent = weakref.proxy(parent)
Problem
I know in Python I can do something like the following: ``` from weakref import ref class A(object): def __init__(self, parent): self._parent = ref(parent) @property def parent(self): return self._parent() a = A(some_other_object) print a.parent ``` In this hypothetical situation, I create an instance of `A` and access the weakly-referenced `parent` object in a nice way. It seems like 4 lines of code per weakly-referenced property is a bit much, though. I thought I could do something like the following as a shortcut to the above: ``` class A(object): def __init__(self, parent): self.parent = property(ref(parent)) ``` but this doesn't return the parent object, it returns a `property` object. Is there a more compact way to create a weakly-referenced object I can access as a property rather than a callable?