Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

5 views
Skip to first unread message

pythonlis...@spamgourmet.com

unread,
Apr 11, 2010, 1:46:28 PM4/11/10
to pytho...@python.org
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:

>>> import inspect, numbers
>>> issubclass(float, numbers.Real)
True
>>> inspect.getmro(float)
(<class 'float'>, <class 'object'>)
>>> float.__bases__
(<class 'object'>,)

Is there a more foolproof way to query this information? Or is this
simply some sort of bug with the new implementation of numbers in
python 2.6?

- Paul

Chris Rebert

unread,
Apr 12, 2010, 12:47:59 AM4/12/10
to pythonlis...@spamgourmet.com, pytho...@python.org
On Sun, Apr 11, 2010 at 10:46 AM, <pythonlis...@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.

Quoting from the `abc` module docs (http://docs.python.org/library/abc.html):
"""[...] You can also register unrelated concrete classes (even
built-in classes) [...] as “virtual subclasses” – these and their
descendants will be considered subclasses of the registering ABC by
the built-in issubclass() function, but the registering ABC won’t
show up in their MRO (Method Resolution Order) nor will method
implementations defined by the registering ABC be callable."""

See also the relevant part of the ABC PEP (PEP 3119):
http://www.python.org/dev/peps/pep-3119/#overloading-isinstance-and-issubclass

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

Steven D'Aprano

unread,
Apr 12, 2010, 2:57:08 AM4/12/10
to
On Sun, 11 Apr 2010 21:47:59 -0700, Chris Rebert wrote:

> On Sun, Apr 11, 2010 at 10:46 AM, <pythonlis...@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

Hans Mulder

unread,
Apr 13, 2010, 3:15:18 PM4/13/10
to
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

Chris Rebert

unread,
Apr 13, 2010, 3:33:57 PM4/13/10
to Hans Mulder, pytho...@python.org

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.

0 new messages