Is type object an instance or class?

René Fleschenberg rene at korteklippe.de
Tue Feb 27 07:59:29 EST 2007


Hi

The article you read at
http://www.cafepy.com/article/python_types_and_objects is really good,
and the most comprehensive one I know about Python's object system. I
recommend that you read it as many times as you need to understand it.
It can be confusing -- just don't give up ;)

I will try to give some answers to your questions. Note that I assume we
are only speaking of new-style classes. Old-style classes are different,
but pretty uninteresting since there is little reason to use them in new
code.

Another note: As far as this stuff in Python is concerened, "type" and
"class" are, to my knowledge, basically just two different names for the
same thing.

JH schrieb:
> Hi
> http://www.cafepy.com/article/python_types_and_objects/
> I found that a type/class are both a subclass and a instance of base
> type "object".
> 
> It conflicts to my understanding that:
> 
> 1.) a type/class object is created from class statement
> 2.) a instance is created by "calling" a class object.

Why? I see no conflict here.

> A object should not be both a class and an instance at the same time.

It should.

1) Everything is an object.
2) Every object is an instance of a class.

>From this, it logically follows that every class should also be an
instance of a class. Classes are objects, and objects are instances of
classes.

> Further I found out there is a special type call "type" that is a
> subclass of base type "object". All other types are instances of this
> type.  Even base type "object" is an instance of this special type.
> 
> What is role of this type "type" in object creation?  Could someone
> there straighten this concept a little?

I) object' is the root of the inheritance hierarchy. All classes inherit
from 'object'. 'object' is its own baseclass. Also, all objects are
instances of 'object'.

II) 'type' is the root of the type hierarchy. All types (i.e. classes)
are subtypes of 'type'. 'type' is its own type.
Because 'type' also is, like everything else, an object, it is an
instance of 'object' (see I). Because it is a type object (a class), it
also is a subclass of 'object' (again, see I).

Objects are created by instantiating their class/type. Classes (also
called "type objects") are usually created by instantiating 'type'.

The object you instantiate to get a class is called that class'
"metaclass". One can just as well call it that class' type.

Unless you specify something else, the metaclass is 'type'. Sometimes it
can be useful to not use the default 'type' as a metaclass, but a class
that inherits from 'type'.

class A(type):
    # Custom code here.
    pass

class B(object):
    __metaclass__ = A

type(B)        # A. We overrode the metaclass.
type(type(B))  # 'type'. The default metaclass.


> For example (Python2.5):
> 
>>>> issubclass(int, object)
> True

All classes are subclasses of 'object'. 'int' is a class, so it is a
subclass of 'object'.

>>>> isinstance(int, object)
> True

All objects are instances of 'object', and 'int' is an object.

>>>> print type(int)
> <type 'type'>

'type' is the default type for classes (the "metaclass"). In the case of
'int', that default was not changed.

>>>> isinstance(int, type)
> True

Since all classes are subclasses of 'object' and 'object' is an instance
of 'type', all classes are instances of 'type'.

>>>> issubclass(int, type)
> False

No reason why it should be. It could be, but it does not have to.

>>>> issubclass(type, object)
> True

'type' is a class, all classes are subclasses of 'object'.

>>>> isinstance(type, object)
> True

'type' is an object, all objects are instances of 'object'.

>>>> isinstance(object, type)
> True

'object' is a class, all classes are instances of 'type'.


Hope this helps ;)

-- 
René



More information about the Python-list mailing list