Python Glossary Русский

Python 3.9+gcfunction

gc.is_finalized()

introspection

Checks whether the collector has already run the object's __del__ finalizer. Useful when debugging 'resurrected' objects.

Syntax

gc.is_finalized(obj)

An object can outlive its __del__ if that method stored self somewhere outside — 'resurrection'. __del__ will not be called for it again, and is_finalized shows that. For objects without __del__ the answer is False even after collection: there was no finalizer. In new code prefer weakref.finalize to __del__ — it runs exactly once and does not resurrect.

Examples

import gc
saved = None
class Lazarus:
    def __del__(self):
        global saved
        saved = self  # воскрешение
obj = Lazarus()
print(gc.is_finalized(obj))  # → False
del obj
print(gc.is_finalized(saved))  # → True — __del__ второй раз не вызовут

See also

Open in the full glossarydocs.python.orgReport an inaccuracy