Reputation: 99526
What is the relation between class type
and specific types?
I thought that specific types were subclasses of type
, but:
>>> type
<class 'type'>
>>> import builtins
>>> builtins.issubclass(type, object)
True
>>> builtins.issubclass(int, type)
False
Thanks.
Upvotes: 0
Views: 60
Reputation: 160637
I thought that specific types were subclasses of type
They aren't. Every class is an instance of type
; type
acts as the class for classes. isinstance(class, type)
returns True
while issubclass
correctly returns False
.
A case where issubclass
returns True
is with custom meta-classes (class of classes) that actually inherit from type
. For example, take EnumMeta
:
>>> from enum import EnumMeta
>>> issubclass(EnumMeta, type)
This is True
because EnumMeta
has type
as a base class (inherits from it):
>>> EnumMeta.__bases__
(type,)
if you looked its source up you'd see it's defined as class EnumMeta(type): ...
.
issubclass(type, object)
returns True
for everything because every single thing in Python is an object (meaning everything inherits from object
).
Upvotes: 4