On this page
  1. weakref.finalize
  2. Import
  3. Signature & Overview
  4. Methods & Attributes
  5. Usage
  6. Gotchas
  7. REPL Inspect

weakref.finalize

Prev: weakref.CallableProxyType | Up: weakref | Next: weakref.getweakrefcount
Rel: weakref.CallableProxyType, weakref.getweakrefcount, weakref.getweakrefs, weakref.proxy
Ref: docs.python.org — weakref.finalize
Tags: #public #python3 #stdlib #weakref


Import

import weakref
from weakref import finalize

Signature & Overview

weakref.finalize(obj, func, /, *args, **kwargs)

Class for finalization of weakrefable objects finalize(obj, func, *args, **kwargs) returns a callable finalizer object which will be called when obj is garbage collected. The

Methods & Attributes

Usage

import weakref

log: list[str] = []
class Res: pass
r = Res()
fin = weakref.finalize(r, log.append, 'cleaned')
del r
assert log == ['cleaned'] and not fin.alive

Gotchas

REPL Inspect

from weakref import finalize
help(finalize)

Linked from

Related notes