Object counts

Martin v. Loewis martin at v.loewis.de
Sat Feb 23 18:20:33 EST 2002


Chuck Esterbrook <ChuckEsterbrook at yahoo.com> writes:

> Is there a way to get Python to report the number of instances of all 
> classes and types? e.g., a dictionary that would map the names of 
> types/classes to their count, which would be incremented on creation 
> and decremented on deletion?

If you compile Python with --with-pydebug, a list of all objects is
maintained at all times. It would be possible to count them on a
per-type basis using this list.

Alternatively, if you are only interested in container objects, you
can use gc.get_objects. From those, you can actually create a list of
all reachable Python objects (including finalizer-reachable
ones). That won't give you objects which are only reachable from C
pointers, or which are garbage due to reference counting bugs.

Regards,
Martin



More information about the Python-list mailing list