Posted in Python onJune 21, 2015
测试代码如下:
class ModelMetaClass(type): def __new__(cls,name,base,attrs): logging.info("cls is:"+str(cls)) logging.info("name is:"+str(name)) logging.info("base is:"+str(base)) logging.info("attrs is:"+str(attrs)) return type.__new__(cls,name,base,attrs) pass pass class Model(dict): __metaclass__ = ModelMetaClass def __init__(self): pass pass def main(): m=Model() if __name__ == '__main__': main()
测试结果:
INFO:root:cls is:<class '__main__.ModelMetaClass'> INFO:root:name is:Model INFO:root:base is:(<type 'dict'>,) INFO:root:attrs is:{'__module__': '__main__', '__metaclass__': <class '__main__.ModelMetaClass'>, '__init__': <function __init__ at 0x025C76F0>}
结论就显而易见了。cls是当前类的名字,即类本身。name是通过__metaclass__属性指向ModelMetaClass的类,即要实例化的类,ModelMetaClass拦截了该类的实例化。base是要实例化的类的基类。attrs是要实例化的类的属性集合。
Python中type的构造函数参数含义说明
- Author -
junjie声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@