Surprisingly, there's no explicit documentation for __weakref__
. Weak references are explained here. __weakref__
is also shortly mentioned in the documentation of __slots__
. But I could not find anything about __weakref__
itself.
What exactly is __weakref__
?
- Is it just a member acting as a flag: If present, the object may be weakly-referenced?
- Or is it a function/variable that can be overridden/assigned to get a desired behavior? How?
__weakref__
is just an opaque object that references all the weak references to the current object. In actual fact it's an instance of weakref
(or sometimes weakproxy
) which is both a weak reference to the object and part of a doubly linked list to all weak references for that object.
It's just an implementation detail that allows the garbage collector to inform weak references that its referent has been collected, and to not allow access to its underlying pointer anymore.
The weak reference can't rely on checking the reference count of the object it refers to. This is because that memory may have been reclaimed and is now being used by another object. Best case scenario the VM will crash, worst case the weak reference will allow access to an object it wasn't originally referring to. This is why the garbage collector must inform the weak reference its referent is no longer valid.
See weakrefobject.h for the structure and C-API for this object. And the implementation detail is here