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

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Mon Apr 12 02:57:08 EDT 2010


On Sun, 11 Apr 2010 21:47:59 -0700, Chris Rebert wrote:

> On Sun, Apr 11, 2010 at 10:46 AM,  <pythonlist.calin79 at spamgourmet.com>
> wrote:
>> Generally, if I want to know the inheritance tree of a class, I either
>> use inspect.getmro or __bases__
>>
>> However, after reading about the new numbers module / class tower in
>> Python 2.6/3.0, I realized that both of these will fail to show that
>> the 'float' type actually inherits from numbers.Real:
> 
> Well, as your introspection shows, it doesn't *actually* inherit from
> numbers.Real.
> However, it does "implement" the numbers.Real Abstract Base Class (ABC)
> and this is reported via the results of issubclass(). Basically,
> `issubclass(x, y)` is no longer more-or-less just sugar for `y in
> x.__mro__` due to changes that have been made to accommodate ABCs.


So this is a "Consenting Adults" inheritance? If I say that MyNumber 
class inherits from numbers.Real, I'm responsible for making sure that 
MyNumber has all the appropriate methods defined by numbers.Real because 
it won't actually inherit any of them.

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.



-- 
Steven



More information about the Python-list mailing list