Search code examples
pythonmemorypython-idle

How to get the list of all initialized objects and function definitions alive in python?


Say that in the python shell (IDLE) I have defined some classes, functions, variables. Also created objects of the classes. Then I deleted some of the objects and created some others. At a later point in time, how can I get to know what are the currently active objects, variables, and methods definitions active in the memory?


Solution

  • Yes.

    >>> import gc
    >>> gc.get_objects()
    

    Not that you'll find that useful. There is a lot of them. :-) Over 4000 just when you start Python.

    Possibly a bit more useful is all the variables active locally:

    >>> locals()
    

    And the one active globally:

    >>> globals()
    

    (Note that "globally" in Python isn't really global as such. For that, you need the gc.get_objects() above, and that you are unlikely to ever find useful, as mentioned).