weakref

Stdlib — IntrospectionPython 2.0+Expert

Weak references that don't prevent garbage collection

Quick Info

Documentation
Official Docs
Python Version
2.0+
Dependencies
None — Python Standard Library
Install
Included with Python

Learn by Difficulty

Quick Example

python
import weakref

class BigObject:
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        return f"BigObject({self.name})"

obj = BigObject("test")
ref = weakref.ref(obj)
print(f"Object: {ref()}")

del obj
print(f"After delete: {ref()}")

The weakref module is part of Python's standard library. Weak references that don't prevent garbage collection.

Try in Playground

Tags

stdlibintrospectionmetaprogramming