Skip to main content

Practice ยท 1 of 1

A cache that does not leak

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.

Difficulty: advanced

Back to lesson: Practice: Memory Practice