type, object hierarchy?

7stud bbxx789_05ss at yahoo.com
Mon Feb 4 06:28:31 CET 2008


On Feb 3, 9:06 pm, Jason <tenax.racc... at gmail.com> wrote:
> On Feb 3, 8:36 pm, 7stud <bbxx789_0... at yahoo.com> wrote:
>
>
>
> > print dir(type)      #__mro__ attribute is in here
> > print dir(object)   #no __mro__ attribute
>
> > class Mammals(object):
> >     pass
> > class Dog(Mammals):
> >     pass
>
> > print issubclass(Dog, type)   #False
> > print Dog.__mro__
>
> > --output:--
> > (<class '__main__.Dog'>, <class '__main__.Mammals'>, <type 'object'>)
>
> > The output suggests that Dog actually is a subclass of type--despite
> > the fact that issubclass(Dog, type) returns False.  In addition, the
> > output of dir(type) and dir(object):
>
> > ['__base__', '__bases__', '__basicsize__', '__call__', '__class__',
> > '__cmp__', '__delattr__', '__dict__', '__dictoffset__', '__doc__',
> > '__flags__', '__getattribute__', '__hash__', '__init__',
> > '__itemsize__', '__module__', '__mro__', '__name__', '__new__',
> > '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
> > '__subclasses__', '__weakrefoffset__', 'mro']
>
> > ['__class__', '__delattr__', '__doc__', '__getattribute__',
> > '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__',
> > '__repr__', '__setattr__', '__str__']
>
> > suggests that type inherits from object since type has all the same
> > attributes as object plus some additional ones.  That seems to
> > indicate a hierarchy like this:
>
> > object
> >  |
> >  V
> > type
> >  |
> >  V
> > Mammals
> >  |
> >  V
> > Dog
>
> > But then why does issubclass(Dog, type) return False?
>
> In your example, Mammal is directly derived from object, and Dog is
> directly derived from Mammal.  Dog isn't derived from type, so it
> isn't considered a subclass.  

From the docs:

issubclass(class, classinfo)
Return true if class is a subclass (direct or indirect) of classinfo.





More information about the Python-list mailing list