Profiling and Memory Modules

weakref

Creates references that do not keep their referents alive. A weak reference returns its object while it exists and None after collection. Weak dictionaries remove entries when weakly referenced objects disappear; not every object type supports weak references. Avoid relying on exact collection timing across implementations.

Formal Syntax & Structure

python syntaxpython
import weakref

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
import weakrefimport gcclass Item: passitem = Item()ref = weakref.ref(item)print(ref() is item)del itemgc.collect()print(ref() is None)
Python Reference