#.metaclass
class Myclass:
pass
print(Myclass.__class__)
print(Myclass.__bases__)
a = 1
print(a.__class__)
class Myclass1:
data = 1
Myclass2 = type('Myclass2', (), {'data': 1})
instance1 = Myclass1()
instance2 = Myclass2()
print(instance1, instance2)
class MyMeta(type):
def __new__(cls, *args, **kwargs):
print('===>MyMeta.__new__')
print(cls.__name__)
return super().__new__(cls, *args, **kwargs)
def __init__(self, classname, superclasses, attributedict):
super().__init__(classname, superclasses, attributedict)
print('===>MyMeta.__init__')
print(self.__name__)
print(attributedict)
print(self.tag)
def __call__(self, *args, **kwargs):
print('===>MyMeta.__call__')
obj = self.__new__(self, *args, **kwargs)
self.__init__(self, *args, **kwargs)
return obj
print('#' * 25)
class Foo(object, metaclass=MyMeta):
tag = '!Foo'
def __new__(cls, *args, **kwargs):
print('===>Foo.__new__')
return super().__new__(cls)
def __init__(self, name):
print('===>Foo.__init__')
self.name = name
print('@' * 25)
foo = Foo('test')