import six from abc import ABCMeta class DictItem(): pass class ItemMeta(ABCMeta): def __new__(mcs, class_name, bases, attrs): print('mcs:%s . class_name:%s . base:%s . attrs:%s '%(mcs,class_name,bases,attrs)) return super(ItemMeta, mcs).__new__(mcs, class_name, bases, attrs) @six.add_metaclass(ItemMeta) class Item(DictItem): pass class Book(Item): name=2 price=3 book2=Book()
输出为:
mcs:<class '__main__.ItemMeta'> . class_name:Item . base:(<class '__main__.DictItem'>,) . attrs:{'__doc__': None, '__module__': '__main__'} //这个是Item类对象构造时的输出 mcs:<class '__main__.ItemMeta'> . class_name:Book . base:(<class '__main__.Item'>,) . attrs:{'__qualname__': 'Book', 'price': 3, '__module__': '__main__', 'name': 2} //这个是实例化book对象的输出