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 gain access to the parent object with a weak reference. It seems that 4 lines of code for a loosely coupled property is a bit, but.
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 does not return the parent object, it returns the property object. Is there a more compact way to create an object with a low reference that I can access, rather than being called?
source share