Python进阶:一步步理解Python中的元类metaclass

注意:Python3中不再有__metaclass__属性以及模块级别的__metaclass__属性。如果一个模块中函数较多,可以利用正则表达式进行查找替换。

不过在平时的使用中,我发现了模块级别有个__build_class__函数,将其改为Author,就能达到模块级别元类的效果。但具体有哪些副作用还不太清楚,慎用!!!

代码地址: LearnPython

最后再强调一遍:

Metaclasses are deeper magic that 99% of users should never worry about. If you wonder whether you need them, you don't (the people who actually need them know with certainty that they need them, and don't need an explanation about why).