Module (27%)
Section (27%)

In Python's approach, everything is an object, and every object has some type associated with it. To get the type of any object, make use of the type() function.

Run the code in the right pane to see the type() function in action.

<class 'int'> <class 'list'> <class '__main__.Dog'> <class 'type'>

output


We can see that objects in Python are defined by their inherent classes.

The example also shows that we can create our own classes, and those classes will be instances of the type special class, which is the default metaclass responsible for creating classes.

Let's perform one more experiment that will respond to the question: what type of objects are built-in classes and the metaclass type?

for t in (int, list, type): print(type(t))

The results are quite interesting:


These observations lead us to the following conclusions:

  • metaclasses are used to create classes;
  • classes are used to create objects;
  • the type of the metaclass type is type – no, that is not a typo.
diagram

To extend the above observations, it’s important to add:

  • type is a class that generates classes defined by a programmer;
  • metaclasses are subclasses of the type class.

Before we start creating our own metaclasses, it’s important to understand some more details regarding classes and the process of creating them.


Code

class Dog:
pass


age = 10
codes = [33, 92]
dog = Dog()

print(type(age))
print(type(codes))
print(type(dog))
print(type(Dog))
{{ dockerServerErrorMsg }} ×
{{ errorMsg }} ×
{{ successMsg }} ×