-1

I am reading Learning python. Regarding metaclass, the book said type is an object and also is itself class. I am trying to search python doc online but don't see any similar description. Can someone point me any official doc?

Thinking a bit more on this statement that type is both object and class, I think it makes sense.

The reasons are as following.

  1. type is callable, seeing type(). Then it should be either a function or object which has implemented call
  2. any object is instanced from a class. Given it's in the top hierarchy, it makes sense that its class is itself.

My second question is that metaclass is subclass of type class? Is it still true that metaclass is also an instance of type object/class? Because when Metaclass(...) is invoked, type.call is invoked then Metaclass must be instance of type

SSY
  • 627
  • 4
  • 10
  • 1
    Please don't ask two separate questions at the same time. One concern per question, please. – Aran-Fey Sep 20 '18 at 16:10
  • Your question is too broad for this site; please ask only one question per post. – Joel Sep 20 '18 at 16:13
  • 1
    Possibly a duplicate of/addressed in answers to this question: https://stackoverflow.com/questions/100003/what-are-metaclasses-in-python – EE_ Sep 20 '18 at 16:13
  • type is a type, but you can't replicate that trick in Python code directly. It's a sleight-of-hand in the implementation. – wim Sep 20 '18 at 16:15
  • Reading this should cover everything in your question: [What are metaclasses in Python?](https://stackoverflow.com/questions/100003/what-are-metaclasses-in-python) – Olivier Melançon Sep 22 '18 at 01:49

1 Answers1

1

Even as a callable, type has two completely different uses. type with one parameter returns the class of an objecdt, while type with 3 arguments creates a new class, which is itself an object of the class type.

Of course, by that logic, type itself could be an instance of another class, and that class an instance of another class. But this had to stop somewhere. The design choice is that it stops there. There is no meta-metaclass. type(type) returns type. This is magic, in the sense that it does not follow from any logical rules, it is just specially implemented.

blue_note
  • 27,712
  • 9
  • 72
  • 90