What does gc.get_objects() return?

Jurko Gospodnetić jurko.gospodnetic at pke.hr
Wed Mar 12 15:34:16 EDT 2014


   Hi all.

   I was wondering if someone could explain gc.get_objects() in a bit 
more detail to me.

   Does it return a list of 'all objects known to Python'? Only some of 
them? Which does it return? Which it does not?

   For example (done using CPython 3.4 interactive interpreter) here it 
does not contain an object I know exists:

> >>> a = object()
> >>> a in gc.get_objects()
> False

   but here it does:

> >>> class Foo: pass
> ...
> >>> a = Foo()
> >>> a in gc.get_objects()
> True

   It also does not seem to contain string objects. At first I thought 
that perhaps interned strings were not placed under GC control, so I 
tested this:

> >>> a = "asdkjfhk23498237$&(*$&*($ksjdhfkjsd"
> >>> b = "asdkjfhk23498237$&(*$&*($ksjdhfkjsd"
> >>> a is b
> False
> >>> a in gc.get_objects()
> False
> >>> b in gc.get_objects()
> False

   Which shows that non-interned scripts such as these are not getting 
listed in gc.get_objects() either. :-s

   Many thanks.

   Best regards,
     Jurko Gospodnetić




More information about the Python-list mailing list