54 – 元类(metaclass)的作用

什么是元类,请用代码解释如何使用元类

'''
metaclass: 元类,类似于创建类的模板,所有的类都是通过他来创建的,可以自由控制类的创建过程

单例模式、ORM模式
'''

class SingLeton(type):
    def __init__(self, *args, **kwargs):
        print('in __init__')
        self.__instance = None
        super(SingLeton, self).__init__(*args, **kwargs)
    def __call__(self, *args, **kwargs):
        print('in __call__')
        if self.__instance == None:
            self.__instance = super(SingLeton, self).__call__(*args, **kwargs)
            print(type(self.__instance))
        return self.__instance
    
class MyClass(metaclass=SingLeton):
    pass

my1 = MyClass()
my2 = MyClass()
print(my1 is my2)

in __init__

in __call__

<class '__main__.MyClass'>

in __call__

True

正文完