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

Chris Rebert clp2 at rebertia.com
Mon Apr 12 00:47:59 EDT 2010


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.

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

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



More information about the Python-list mailing list