There is no cross-inheritance between object
and type
. In fact, cross-inheritance is impossible.
# A type is an object
isinstance(int, object) # True
# But an object is not necessarily a type
isinstance(object(), type) # False
What is true in Python is that...
Everything is an object
Absolutly everything, object
is the only base type.
isinstance(1, object) # True
isinstance('Hello World', object) # True
isinstance(int, object) # True
isinstance(object, object) # True
isinstance(type, object) # True
Everything has a type
Everything has a type, either built-in or user-defined, and this type can be obtained with type
.
type(1) # int
type('Hello World') # str
type(object) # type
Not everything is a type
That one is fairly obvious
isinstance(1, type) # False
isinstance(isinstance, type) # False
isinstance(int, type) # True
type
is its own type
This is the behaviour which is specific to type
and that is not reproducible for any other class.
type(type) # type
In other word, type
is the only object in Python such that
type(type) is type # True
# While...
type(object) is object # False
This is because type
is the only built-in metaclass. A metaclass is simply a class, but its instances are also classes themselves. So in your example...
# This defines a class
class Foo(object):
pass
# Its instances are not types
isinstance(Foo(), type) # False
# While this defines a metaclass
class Bar(type):
pass
# Its instances are types
MyClass = Bar('MyClass', (), {})
isinstance(MyClass, type) # True
# And it is a class
x = MyClass()
isinstance(x, MyClass) # True