Weak References
Talk it through out loud with your AI coach.
The problem
- medium
- Pro
Your service caches expensive objects in a module-level dictionary keyed by ID so they can be shared, but the cache keeps every object alive forever, even ones nothing else uses. Explain how weak references could help and their limitations.
What a strong answer covers
- Why an ordinary cache prevents objects from ever being freed.
- What makes a weak reference different from a normal one.
- A ready-made container suited to this cache.
- Handling the case where the object has already gone, and what can't be referenced weakly.
Common mistake: Forgetting that a weak reference can come back empty and using its result as if the object were always there.
Knowing this is half of it. Saying it clearly out loud is the part the coach helps you practise.
Practise it out loud