自定义元类

class MyType(type):
    def __init__(self, a, b, c):
        print('元类构造函数执行')

    def __call__(self, *args, **kwargs):
        obj = object.__new__(self)
        self.__init__(obj, *args, **kwargs)
        return obj


class Foo(metaclass=MyType):
    def __init__(self, name):
        self.name = name


print(Foo)


f = Foo('alex')

 

posted @ 2019-01-15 09:56  我爱敲代码  阅读(114)  评论(0编辑  收藏  举报