[Python] gc.get_referrers returns a list of dictionaries

Discussão em 'Python' iniciado por Stack, Maio 22, 2025.

  1. Stack

    Stack Membro Participativo

    As far as I understood, gc.get_referrers(my_obj) should return a list of the objects that refer to my_obj. However, I'm currently seeing this behaviour:

    import sys
    import gc

    my_obj = []
    ref_1 = my_obj
    ref_2 = my_obj
    sys.getrefcount(my_obj) # Returns 4, as expected

    gc.get_referrers(my_obj)


    This last command returns the following:

    [
    {
    '__name__': '__main__',
    '__doc__': None,
    '__package__': None,
    '__loader__': <class '_frozen_importlib.BuiltinImporter'>,
    '__spec__': None,
    '__annotations__': {},
    '__builtins__': <module 'builtins' (built-in)>,
    'sys': <module 'sys' (built-in)>,
    'gc': <module 'gc' (built-in)>,
    'my_obj': [],
    'ref_1': [],
    'ref_2': []
    }
    ]


    I was expecting to receive a list of 4 objects, but gc.get_referrers(my_obj) is returning a list that contains only one dictionary instead.

    What does this dictionary represent? Where is it documented? And why is gc.get_referrers(my_obj) returning it instead of the 4 objects I was expecting?

    Continue reading...

Compartilhe esta Página