Wow: list of immediate subclasses?

Andrew Dalke adalke at mindspring.com
Fri Aug 1 10:56:01 CEST 2003


Carl Banks, responding to Roman Suzi:
> __subclasses__ is a list of weak references, so the class can be
> collected even though it's listed in __subclasses__.  For whatever
> reason, class A isn't deleted by reference counts, but cyclic garbage
> collection gets it.

But I want to know that reason.  Why are there any cycles?  After
all, don't weak-references help break cycles?  And in my code
before, why do I need to do the gc a few times to force the cycle
to be broken?

>>> float.__subclasses__()
[]
>>> class Spam(float): pass
...
>>> float.__subclasses__()
[<class '__main__.Spam'>]
>>> del Spam
>>> _ = None  # just in case ...
>>> float.__subclasses__()
[<class '__main__.Spam'>]
>>> gc.collect()
0
>>> float.__subclasses__()
[<class '__main__.Spam'>]
>>> gc.collect()
0
>>> float.__subclasses__()
[<class '__main__.Spam'>]
>>> gc.collect()
0
>>> gc.collect()
6
>>> gc.collect()
0
>>> float.__subclasses__()
[]
>>>

It's a bit too mysterious for my liking.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list