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

Discussion in 'Python' started by Chris Rebert, Apr 12, 2010.

  1. Chris Rebert

    Chris Rebert Guest

    On Sun, Apr 11, 2010 at 10:46 AM, <> 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'>,)
    Chris Rebert, Apr 12, 2010
    #1
    1. Advertising

  2. Re: A 'foolproof' way to query inheritance tree? numbers.Real in2.6)

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

    > On Sun, Apr 11, 2010 at 10:46 AM, <>
    > 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
    Steven D'Aprano, Apr 12, 2010
    #2
    1. Advertising

  3. Chris Rebert

    Hans Mulder Guest

    Re: A 'foolproof' way to query inheritance tree? numbers.Real in2.6)

    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
    Hans Mulder, Apr 13, 2010
    #3
  4. Chris Rebert

    Chris Rebert Guest

    On Tue, Apr 13, 2010 at 12:15 PM, Hans Mulder <> 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
    Chris Rebert, Apr 13, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,090
  2. Curious Trigger
    Replies:
    2
    Views:
    1,793
    Curious Trigger
    Sep 9, 2006
  3. Replies:
    0
    Views:
    300
  4. Paul Mckibbin
    Replies:
    5
    Views:
    152
    Aldric Giacomoni
    Jan 15, 2010
  5. Replies:
    4
    Views:
    228
    Arne Vajh?j
    Apr 4, 2013
Loading...

Share This Page