Closed mentalisttraceur closed 2 years ago
Ah! No. I'm wrong.
The other nice use-case for weak references is caches which can automatically forget their contents.
And this is a good use-case for macaddress
classes to support. For example, what if you want to have a WeakKeyDictionary
with MAC addresses as the keys?
More of the design considerations are coming back to me now:
There is a decent outside-the-library solution - you can always wrap a non-weakly-referenceable thing to make a weakly-referenceable thing. You could subclass and add __slots__ = ('__weakref__',)
in the subclass, or you could create a weakly referenceable wrapper.
These are decent, general, composable solutions.
Also, the reversibility heuristic weakly suggests not allowing weak references in the base class, because if you want to for any reason take away weak-referenceability, you can't directly do so.
But the only good use-case I can think of for not supporting weak references is micro-optimization: not paying the overhead for one more slot for __weakref__
. And if you're in a situation where you got that kind of performance overhead concern, I think nowadays my verdict on that is "use or create a more optimized/optimizing implementation of Python, or a transpiler, or some tooling to apply the "remove weak reference slots from classes because we know we aren't using them and the overhead matters" optimization automatically.
Okay, new verdict: I'll add the __weakref__
slot, and if you ever run into a situation where it seems like the best optimization would be to remove that slot, let me know, and that might motivate me to spend some time and effort on a tool which does that automatically.
If you have a use-case for weakly referencing classes provided by this library, let me know.
I don't see a use-case for it, because weak references exist to break reference cycles, but the classes provided by this library don't really reference anything.
So I've been holding off on adding
__weakref__
to__slots__
, because it seems pointless. And I kinda want to wait and see if it ever actually comes up.But I'm open to adding it if anyone runs into a good use-case for it.