Based on my limited Python knowledge Metaclass is another class. In the following example I am just building a metaclass. Here I overloaded the __new__ method. My expectation is at the end of the class declaration when type.__new__() is called it will be delegated to MyMeta.__new__(). But that never happens.
print('Building MetaOne') class MyMeta(type): def __new__(meta, classname, supers, classdict): print('In MyMeta:', classname) def myfunc(self): print ('In myfunc') Here from MyMeta's __new__() I don't see the print.
But on the contrary if I now use MyMeta class as a meta class to another class like:
print('Building Another class') class Super(metaclass = MyMeta): def myfunc(self): print ('In Super myfunc') At the end of this class I see MyMeta's __new__ is getting invoked.
Can some one please help me understand why this happens?
__new__is not called when you created the meta class; .Superis an instance of the metatype, a class object, so then__new__is called to create it. Note that your__new__method returnsNone, rather than a new class object. You may want to addreturn super().__new__(meta, classname, supers, classdict)to the end of it.__new__on a class definition as well, and it would only be called when you call the class to create new instances.type(). You can't create meta metaclasses.