[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

type, object hierarchy?

7stud --

2/4/2008 3:36:00 AM

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?
23 Answers

Jason

2/4/2008 4:06:00 AM

0

On Feb 3, 8:36 pm, 7stud <bbxx789_0...@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. However, Python does create an instance
of class 'type' for each class you define. That doesn't mean that
'type' is a superclass. Try "isinstance(Dog, type)": it will return
True.

Type does inherit from object, but that's not what you're deriving
from. The hierarchy that you're using is:

Dog
^
|
Mammal
^
|
object

As you notice, type isn't part of the object hierarchy for Dog.
That's why it doesn't show up in the MRO. If you want to make Dog a
subclass of type, you'd need to do:

>>> class Dog(Mammal, type):
.... pass
....
>>> issubclass(Dog, type)
True

I don't know why'd you would want to do that, though.

--Jason

7stud --

2/4/2008 5:29:00 AM

0

On Feb 3, 9:06 pm, Jason <tenax.racc...@gmail.com> wrote:
> On Feb 3, 8:36 pm, 7stud <bbxx789_0...@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.


7stud --

2/4/2008 5:32:00 AM

0

On Feb 3, 10:28 pm, 7stud <bbxx789_0...@yahoo.com> wrote:
> From the docs:
>
> issubclass(class, classinfo)
> Return true if class is a subclass (direct or indirect) of classinfo.


print issubclass(Dog, object) #True
print issubclass(type, object) #True
print issubclass(Dog, type) #False

Robert Kern

2/4/2008 5:59:00 AM

0

7stud wrote:
> On Feb 3, 10:28 pm, 7stud <bbxx789_0...@yahoo.com> wrote:
>> From the docs:
>>
>> issubclass(class, classinfo)
>> Return true if class is a subclass (direct or indirect) of classinfo.
>
> print issubclass(Dog, object) #True
> print issubclass(type, object) #True
> print issubclass(Dog, type) #False

And if you want to really blow your mind,

print isinstance(type, object) # True
print isinstance(object, type) # True

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

I V

2/4/2008 6:32:00 AM

0

On Sun, 03 Feb 2008 21:31:44 -0800, 7stud wrote:

> On Feb 3, 10:28 pm, 7stud <bbxx789_0...@yahoo.com> wrote:
>> From the docs:
>>
>> issubclass(class, classinfo)
>> Return true if class is a subclass (direct or indirect) of classinfo.
>
>
> print issubclass(Dog, object) #True

So Dog is a subclass of object

> print issubclass(type, object) #True

and type is also a subclass of object. But

print issubclass(object, type) # False

so

> print issubclass(Dog, type) #False

which is what you would expect - Dog is a subclass of object, and object
isn't a subclass of type, so Dog isn't a subclass of type either.

Arnaud Delobelle

2/4/2008 7:22:00 AM

0

On Feb 4, 5:31 am, 7stud <bbxx789_0...@yahoo.com> wrote:
> On Feb 3, 10:28 pm, 7stud <bbxx789_0...@yahoo.com> wrote:
>
> > From the docs:
>
> > issubclass(class, classinfo)
> > Return true if class is a subclass (direct or indirect) of classinfo.
>
> print issubclass(Dog, object)  #True
> print issubclass(type, object)  #True
> print issubclass(Dog, type)   #False

Are you suggesting a new axiom for propositional logic:

((P => Q) ^ (R => Q)) => (P => R) ?

I.e. in fruit logic: every orange is a fruit and every apple is a
fruit, therefore every orange is an apple.

--
Arnaud

Ben Finney

2/4/2008 7:32:00 AM

0

Robert Kern <robert.kern@gmail.com> writes:

> And if you want to really blow your mind,
>
> print isinstance(type, object) # True
> print isinstance(object, type) # True

Not what I see here.

Python 2.4.4 (#2, Jan 3 2008, 13:39:07)
[GCC 4.2.3 20071123 (prerelease) (Debian 4.2.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> issubclass(type, object)
True
>>> issubclass(object, type)
False

Python 2.5.2a0 (r251:54863, Jan 3 2008, 19:40:30)
[GCC 4.2.3 20071123 (prerelease) (Debian 4.2.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> issubclass(type, object)
True
>>> issubclass(object, type)
False

--
\ "Ignorance more frequently begets confidence than does |
`\ knowledge." â??Charles Darwin |
_o__) |
Ben Finney

Ben Finney

2/4/2008 7:40:00 AM

0

Ben Finney <bignose+hates-spam@benfinney.id.au> writes:

> Robert Kern <robert.kern@gmail.com> writes:
>
> > And if you want to really blow your mind,
> >
> > print isinstance(type, object) # True
> > print isinstance(object, type) # True
>
> Not what I see here.

Ah, you tricked me! The parent message to yours was talking about
'issubclass' but you switched to 'isinstance'.

Nothing to see here.

--
\ "Never do anything against conscience even if the state demands |
`\ it." â??Albert Einstein |
_o__) |
Ben Finney

Terry Jones

2/4/2008 7:40:00 AM

0

>>>>> "Arnaud" == Arnaud Delobelle <arnodel@googlemail.com> writes:

Arnaud> Are you suggesting a new axiom for propositional logic:
Arnaud> ((P => Q) ^ (R => Q)) => (P => R) ?

Arnaud> I.e. in fruit logic: every orange is a fruit and every apple is a
Arnaud> fruit, therefore every orange is an apple.

This is otherwise known as Winterson's law:

1. Every orange is a fruit
2. Every apple is a fruit

=> Oranges are not the only fruit

Terry

Terry Jones

2/4/2008 7:40:00 AM

0

>>>>> "Arnaud" == Arnaud Delobelle <arnodel@googlemail.com> writes:

Arnaud> Are you suggesting a new axiom for propositional logic:
Arnaud> ((P => Q) ^ (R => Q)) => (P => R) ?

Arnaud> I.e. in fruit logic: every orange is a fruit and every apple is a
Arnaud> fruit, therefore every orange is an apple.

This is otherwise known as Winterson's law:

1. Every orange is a fruit
2. Every apple is a fruit

=> Oranges are not the only fruit

Terry