Skip to main content

Practice · 1 of 1

A cache that does not leak

CHALLENGE
Difficulty: advanced+25 XP

Implement class WeakCache: - put(key, obj) stores a WEAK reference to obj under key - get(key) returns the live object, or None once it has been garbage-collected (or was never put) - optional default_factory(key) builds and caches the object on a miss (also weakly) Graded probe: objects are created with no strong references left; after gc.collect(), get must return None — a strong-reference cache fails this.

Back to lesson: Practice: Memory Practice