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

Chris Rebert clp2 at rebertia.com
Tue Apr 13 21:33:57 CEST 2010


On Tue, Apr 13, 2010 at 12:15 PM, Hans Mulder <hansmu at xs4all.nl> wrote:
> 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.

Being pedantic here, but your last statement is the wrong way around:
C is the one who decides which classes should be considered its
subclasses.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list