Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: A 'foolproof' way to query inheritance tree? numbers.Real in 2.6)

Reply
Thread Tools

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

 
 
Chris Rebert
Guest
Posts: n/a
 
      04-12-2010
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-3...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'>,)

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      04-12-2010
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
 
Reply With Quote
 
 
 
 
Hans Mulder
Guest
Posts: n/a
 
      04-13-2010
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
 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      04-13-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A 'foolproof' way to query inheritance tree?numbers.Real in 2.6) pythonlist.calin79@spamgourmet.com Python 0 04-11-2010 05:46 PM
Interface inheritance vs Implementation inheritance. Daniel Pitts Java 27 02-27-2008 01:37 AM
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57