Is type object an instance or class?
Peter Otten
__peter__ at web.de
Tue Feb 27 04:09:45 EST 2007
JH wrote:
> 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.
>
> A object should not be both a class and an instance at the same time.
A class should be an instance. Now what?
> 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?
The type of a class is called metaclass. The creation of a class is the same
as an instantiation of its metaclass. You can therefore write
class A(object):
answer = 42
as
A = type("A", (object,), dict(answer=42))
So now we know how to make a class from a metaclass, how can we make a
metaclass? The tailbiting answer is that a metaclass is a class, too.
Can you figure out the result of isinstance(type, type)?
Peter
More information about the Python-list
mailing list