深入源码解析Python中的对象与类型
对象 Python中对象分为两类: 定长(int等),非定长(list/dict等) 所有对象都有一些相同的东西,源码中定义为PyObject和PyVarObject,两个定义都有一个共同的头部定义PyObject_HEAD(其实PyVarObject有自己的头部定义PyObject_VAR_HEAD,但其实际上用的也是PyObject_HEAD). 源码位置: Include/object.h PyObject_HEAD 定义 /* PyObject_HEAD defines the initial segment of every PyObject. */ #define PyObject_HEAD _PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt; struct _typeobject *ob_type; 说明 1. _PyObject_HEAD_EXTRA 2. Py_ssize_t ob_refcnt 3. struct _typeobject *ob_type typedef struct _object { PyObject_HEAD } PyObject; 说明 1. 依赖关系 PyVarObject typedef struct { PyObject_VAR_HEAD } PyVarObject; #define PyObject_VAR_HEAD PyObject_HEAD Py_ssize_t ob_size; /* Number of items in variable part */ 说明 1. 依赖关系 2.Py_ssize_t ob_size 代码关系 几个方法 #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt) #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) #define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size) Py_INCREF(op) 增加对象引用计数 Py_DECREF(op) 减少对象引用计数,如果计数位0,调用_Py_Dealloc _Py_Dealloc(op) 调用对应类型的 tp_dealloc 方法(每种类型回收行为不一样的,各种缓存池机制,后面看) ob_refcnt 引用计数,与内存管理/垃圾回收相关
>>> a = 1 >>> a 1 >>> type(a) <type 'int'> #等价的两个 >>> type(type(a)) <type 'type'> >>> type(int) <type 'type'> #还是等价的两个 >>> type(type(type(a))) <type 'type'> >>> type(type(int)) <type 'type'> 我们反向推导一个int对象是怎么生成的. 1. 首先,定义一种类型叫PyTypeObject 定义
typedef struct _typeobject { /* MARK: base,注意,是个变长对象*/ PyObject_VAR_HEAD const char *tp_name; /* For printing,in format "<module>.<name>" */ //类型名 Py_ssize_t tp_basicsize,tp_itemsize; /* For allocation */ // 创建该类型对象时分配的内存空间大小 // 一堆方法定义,函数和指针 /* Methods to implement standard operations */ printfunc tp_print; hashfunc tp_hash; /* Method suites for standard classes */ PyNumberMethods *tp_as_number; // 数值对象操作 PySequenceMethods *tp_as_sequence; // 序列对象操作 PyMappingMethods *tp_as_mapping; // 字典对象操作 // 一堆属性定义 .... } PyTypeObject; 说明 1. PyObject_VAR_HEAD 2. const char *tp_name 2. 然后,用PyTypeObject初始化得到一个对象PyType_Type 定义 PyTypeObject PyType_Type = { PyVarObject_HEAD_INIT(&PyType_Type,0) "type",/* tp_name */ sizeof(PyHeapTypeObject),/* tp_basicsize */ sizeof(PyMemberDef),/* tp_itemsize */ (destructor)type_dealloc,/* tp_dealloc */ // type对象的方法和属性初始化值 ..... }; 说明 1. tp_name 2. PyVarObject_HEAD_INIT(&PyType_Type,0) 即,PyType_Type的类型是其本身! 第一张图,箭头表示实例化(google doc用不是很熟找不到对应类型的箭头) 第二张图,箭头表示指向 使用 # 1. int 的 类型 是`type` >>> type(int) <type 'type'> # 2. type 的类型 还是`type`,对应上面说明第二点 >>> type(type(int)) <type 'type'> 注意: 无论任何时候,ob_type指向的是 PyTypeObject的实例: PyType_Type/PyInt_Type... 3. 再然后,定义具体的类型,这里以PyInt_Type为例子 定义 PyTypeObject PyInt_Type = { PyVarObject_HEAD_INIT(&PyType_Type,0) "int",sizeof(PyIntObject),// int类型的相关方法和属性值 .... (hashfunc)int_hash,/* tp_hash */ }; 说明 1. "int" 2.PyVarObject_HEAD_INIT(&PyType_Type,0)
*ob_type = &PyType_Type 结构 使用 >>> type(1) <type 'int'> >>> type(type(1)) <type 'type'> 4. 最后,生成一个整数对象int 定义 typedef struct { PyObject_HEAD long ob_ival; } PyIntObject; 结构 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |