Subclassing Type Vs Object In Python3
Solution 1:
There is no cross-inheritance between object
and type
. In fact, cross-inheritance is impossible.
# A type is an objectisinstance(int, object) # True# But an object is not necessarily a typeisinstance(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) # Trueisinstance('Hello World', object) # Trueisinstance(int, object) # Trueisinstance(object, object) # Trueisinstance(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) # inttype('Hello World') # strtype(object) # type
Not everything is a type
That one is fairly obvious
isinstance(1, type) # Falseisinstance(isinstance, type) # Falseisinstance(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 X
in Python such that type(X) is X
type(type) istype# True# While...type(object) isobject# 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 classclassFoo(object):
pass# Its instances are not typesisinstance(Foo(), type) # False# While this defines a metaclassclassBar(type):
pass# Its instances are types
MyClass = Bar('MyClass', (), {})
isinstance(MyClass, type) # True# And it is a class
x = MyClass()
isinstance(x, MyClass) # True
Solution 2:
In Python everything is an object. Also every object has a type. In fact the type of an object is also an object and therefore must also have its own type. Types have a special type that is called type
. This (like any other type) is an object and is therefore an instance of object
.
Every object is an instance of object
including any type of any object. So int
is an object and so is str
as well as more obvious examples such as 1
and 'asd'
. Anything that you can refer to or assign to a variable in Python is an instance of object
.
Since object
is a type it is an instance of type
. This means that object
and type
are both instances of each other. This is not "inheritance" regardless of what the other answer you linked says. The relationship is the same as the relationship between int
and 1
: the object resulting from 1
is an instance of int
. The quirk here is that object
and type
are both instances of each other.
From a Python perspective these two mean different things. The object
type has an ontological role: everything is an object (and nothing else exists). So to say that type
is an object just means that it exists as far as Python's model is concerned. On the other hand object
is the base type of all objects so it is a type. As a type it must be an instance of type
which like any other object is an instance of object
.
As far as the interpreters are implemented: the fact that type
is an instance of object
is convenient since it maintains "everything is an object" which is useful for e.g. deallocating objects at shutdown. The fact that object
is an instance of type
is useful since it makes it straight-forward to ensure that it behaves like other type objects.
Post a Comment for "Subclassing Type Vs Object In Python3"