A 'foolproof' way to query inheritance tree? numbers.Real in 2.6)

Hans Mulder hansmu at xs4all.nl
Tue Apr 13 21:15:18 CEST 2010

Steven D'Aprano wrote:

> Given a class C, is there some way to find out what classes
> issubclass(C, X) will return true for? Obviously you can get a partial 
> list, by walking the MRO, but is there a list somewhere of which ABCs 
> consider C a subclass?
> Presumably the general answer is No, because any class X could happen to 
> have a __subclasscheck__ method that returns True when called with C.

New style class method __subclasses__ that returns a list of the immediate
subclasses.  By recursing down from object, you can make a list of all new
style classes in your current interpreter.  Then use issubclass to find
the ones that consider themselves subclasses of C.

-- HansM

More information about the Python-list mailing list