0

i am new in python and having some confusions. As Java, in python also it is showing that Object is the top-most class but then what is the use of Metaclass as it is showing that all classes are objects of Metaclass. I am getting confused to grsap it. Please help

I am trying to clear my concept on Metaclass and Object class.

1
  • 1
    Metaclasses and superclasses are different concepts. Metaclass is the class of the class. Commented Jun 5, 2024 at 22:35

1 Answer 1

2

Don't try too hard to grasp it - there is a "paradox" there, indeed.

object being a class in Python has a metaclass, and it is type: type is the default metaclass for everything.

But, being a class, type is also a subclass of object. And also, as type needs a metaclass itself, that metaclass is also type: it is its own metaclass.

These relationships are possible becuse type and object are wired like so in the source code in C for Python itself: one can't establish similar relationships for user defined classes.

It is not too important to understand that completely - the important thing to understand is that a "metaclass" relationship - the "class of a class" is not the same as a "superclass" or inheritance: these are ortogonal concepts, and one can live a whole life as a programmer without ever touching the metaclass concept.


In [24]: object.__mro__
Out[24]: (object,)

In [25]: type.__mro__
Out[25]: (type, object)

In [26]: type(object)
Out[26]: type

In [27]: type(type)
Out[27]: type
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.