PyDocs

Nav

Data Types

finalize

weakref

class · Added in 3.4 (source)
  • 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
first time the finalizer is called it evaluates func(*arg, **kwargs)
and returns the result. After this the finalizer is dead, and
calling it just returns None.

When the program exits any remaining finalizers for which the
atexit attribute is true will be run in reverse order of creation.
By default atexit is true.
Members of finalize

_

__call__
If alive then mark as dead and return func(*args, **kwargs); otherwise return None

a

alive
Whether finalizer is alive
atexit
Whether finalizer should be called at exit

d

detach
If alive then mark as dead and return (obj, func, args, kwargs); otherwise return None

p

peek
If alive then return (obj, func, args, kwargs); otherwise return None
1 Example
>>> import weakref
>>> class Object:
...     pass
...
>>> kenny = Object()
>>> weakref.finalize(kenny, print, "You killed Kenny!")
<finalize object at ...; for 'Object' at ...>
>>> del kenny
You killed Kenny!
from the Python docs
See Also
No see-alsos for weakref/finalize
0 Notes
No notes for finalize