__subclasses__() (was OK, now *this* is cool)

Ype Kingma ykingma at accessforall.nl
Sun Dec 9 05:43:29 EST 2001


Martin,

you wrote:
> 
> Ype Kingma <ykingma at accessforall.nl> writes:
> 
> > P.S. Did they really make __subclasses__() a method?
> 
> Yes. Each type holds a list of weak references to its subtypes, to
> avoid creating cycles. If you want a list of all subclasses, you need
> to go through the weak references and find those which are still
> live. The list you get will be different from what is in the type
> object; the function call will remind you that some computation is
> going on.
> 

Thanks for your explanation. It took some time to digest.

Do I understand correctly that subclass references are weak links
because a subclass may disappear (become not live) and a normal subclass
reference could then keep the subclass accessible only because the
of the existence of the normal subclass reference in the super class?

To avoid this problem (superfluous reference to subclass) weak links are
used so that the garbage collector can free the subclass when expected.
When a real reference is needed as a result if the __subclasses__() method
this method then checks all weak references and (atomically)
creates normal references from the ones that are still valid.

Superclass references should be normal references because they
are needed to find attributes by inheritance.

I suppose the cycles you mention are not direclty related to the former
python problem that the garbage collector could not get rid of objects
in cycles?

Thanks in advance,
Ype

P.S. I'll also take a look at python-dev.



More information about the Python-list mailing list