Python Glossary Русский

all Python 3gcfunction

gc.get_referrers()

introspection

Returns the containers that directly refer to the given object. Helps find out who keeps an object alive.

Syntax

gc.get_referrers(*objs)

The answer nearly always contains 'extra' references: the module globals dict, the current function's frame. Dead cycles not yet collected appear too — call gc.collect first. Walking every object is slow, and the found objects may be half-constructed, so the function is for debugging only, never for program logic.

Examples

import gc
item = ['data']
holder = [item]
print(any(r is holder for r in gc.get_referrers(item)))  # → True
print(any(r is globals() for r in gc.get_referrers(item)))  # → True — глобальные тоже держат

See also

Open in the full glossarydocs.python.orgReport an inaccuracy