Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > type, object hierarchy?

Reply
Thread Tools

type, object hierarchy?

 
 
7stud
Guest
Posts: n/a
 
      02-04-2008
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?
 
Reply With Quote
 
 
 
 
Jason
Guest
Posts: n/a
 
      02-04-2008
On Feb 3, 8:36 pm, 7stud <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
7stud
Guest
Posts: n/a
 
      02-04-2008
On Feb 3, 9:06*pm, Jason <(E-Mail Removed)> wrote:
> On Feb 3, 8:36 pm, 7stud <(E-Mail Removed)> 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.


 
Reply With Quote
 
7stud
Guest
Posts: n/a
 
      02-04-2008
On Feb 3, 10:28*pm, 7stud <(E-Mail Removed)> 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
 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      02-04-2008
7stud wrote:
> On Feb 3, 10:28 pm, 7stud <(E-Mail Removed)> 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

 
Reply With Quote
 
I V
Guest
Posts: n/a
 
      02-04-2008
On Sun, 03 Feb 2008 21:31:44 -0800, 7stud wrote:

> On Feb 3, 10:28Â*pm, 7stud <(E-Mail Removed)> 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.

 
Reply With Quote
 
Arnaud Delobelle
Guest
Posts: n/a
 
      02-04-2008
On Feb 4, 5:31*am, 7stud <(E-Mail Removed)> wrote:
> On Feb 3, 10:28*pm, 7stud <(E-Mail Removed)> 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

 
Reply With Quote
 
Ben Finney
Guest
Posts: n/a
 
      02-04-2008
Robert Kern <(E-Mail Removed)> 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
 
Reply With Quote
 
Ben Finney
Guest
Posts: n/a
 
      02-04-2008
Ben Finney <(E-Mail Removed)> writes:

> Robert Kern <(E-Mail Removed)> 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
 
Reply With Quote
 
Terry Jones
Guest
Posts: n/a
 
      02-04-2008
>>>>> "Arnaud" == Arnaud Delobelle <(E-Mail Removed)> 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
 
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
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Error:Object reference not set to an instance of an object. Suresh Kojhani ASP .Net 1 07-29-2004 12:10 PM
Error !Object reference not set to an instance of an object. !!! Help Parthiv Joshi ASP .Net 2 07-02-2004 10:28 AM
Object reference not set to an instance of an object. yysiow ASP .Net 1 07-12-2003 03:30 PM
Object reference not set to an instance of an object. Chris Fink ASP .Net 2 07-03-2003 06:48 PM



Advertisments