0

Is there a single root type that all other types inherit from in python? From doing basic trial-and-error, it seems this may be the type called type:

>>> None.__class__
<class 'NoneType'>
>>> None.__class__.__class__
<class 'type'>
>>> None.__class__.__class__.__class__
<class 'type'>

And also it seems like that is the only type for which the class of it equals itself.

Are there any other types that are more basic than the type type? If not, why is type the most basic type in python?

FI-Info
  • 635
  • 7
  • 16

2 Answers2

1

'type' is something known as a metaclass in python.

metaclasses are a little different from classes. Like classes create instances of objects, metaclasses create instance of classes. Python is a little different than other languages when it comes to classes. In Python, a class is also an object. This object has the ability to create more objects so, it is a class.

But still, it's an object, and therefore:

  1. you can assign it to a variable
  2. you can copy it
  3. you can add attributes to it
  4. you can pass it as a function parameter

This is a good article that talks about it in more detail. Also, this SO question could also be useful.

Are there any other types that are most basic than the type type?

Yes, In fact, you can create your own metaclasses. To do that, you have to create a class derived from the class 'type'. Refer to the links for a more in depth answer.

Sanil Khurana
  • 1,129
  • 9
  • 20
1

You can find the answer at exact anwer at https://docs.python.org/3.8/reference/datamodel.html#objects.

In short, most basic type of all Python obejct is Class 'object'.

You can simply check this way in python console

>>> help(object)
Help on class object in module builtins:

class object
 |  The most base type


>>> issubclass(type,object)
True
kdw9502
  • 105
  • 6