Python 元类 __new__ 方法没有被调用

py2*_*016 6 python metaclass operator-overloading python-3.x

基于我有限的 Python 知识,Metaclass 是另一个类。在下面的例子中,我只是在构建一个元类。在这里,我重载了该__new__方法。我的期望是在类声明的末尾,当type.__new__() 被调用时它将被委托给MyMeta.__new__(). 但这永远不会发生。

print('Building MetaOne')

class MyMeta(type):
      def __new__(meta, classname, supers, classdict):
          print('In MyMeta:', classname)

      def myfunc(self):
          print ('In myfunc')
Run Code Online (Sandbox Code Playgroud)

这里距MyMeta__new__()我没有看到打印。

但相反,如果我现在使用MyMeta类作为另一个类的元类,例如:

print('Building Another class')
class Super(metaclass = MyMeta):
      def myfunc(self):
          print ('In Super myfunc')
Run Code Online (Sandbox Code Playgroud)

在这门课结束时,我看到 MyMeta__new__被调用了。

有人可以帮助我理解为什么会发生这种情况吗?