Python源码学习-PyTypeObject

重点学习下一个数据结构
    PyTypeObject PyType_Type = {
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
    "type",                                     /* tp_name */
    sizeof(PyHeapTypeObject),                   /* tp_basicsize */
	...
}

#define PyVarObject_HEAD_INIT(type, size)       \
    { PyObject_HEAD_INIT(type) size },
	
等价

PyTypeObject PyType_Type = {
    { PyObject_HEAD_INIT(&PyType_Type) 0 },
    "type",                                     /* tp_name */
    sizeof(PyHeapTypeObject),                   /* tp_basicsize */
		...
}

#define PyObject_HEAD_INIT(type)        \
    { _PyObject_EXTRA_INIT              \
    1, type },
	
等价

PyTypeObject PyType_Type = {
    { { _PyObject_EXTRA_INIT            
    1, &PyType_Type },0 },
    "type",                                     /* tp_name */
    sizeof(PyHeapTypeObject),                   /* tp_basicsize */
		...
}

等价

PyTypeObject PyType_Type = {
    { { 0,  0,            
    1, &PyType_Type },0 },
    "type",                                     /* tp_name */
    sizeof(PyHeapTypeObject),                   /* tp_basicsize */
		...
}

根据PyTypeObject的定义可以看出
    { { 0,  0,            
    1, &PyType_Type },0 } 
就是定义里面的 PyVarObject ob_base

typedef struct {
    PyObject ob_base;
    Py_ssize_t ob_size; /* Number of items in variable part */
} PyVarObject;

最后可以解释为
    { 
	0, 				/*struct _object *_ob_next;*/
	0,       		/*struct _object *_ob_prev;*/     
    1, 				/*Py_ssize_t ob_refcnt;*/
	&PyType_Type , 	/*struct _typeobject *ob_type;*/
	0    			/* ob_size ^/
	} 
	
结合前一篇的类图,我们可以看到PyTypeObject对象里面,有一个ob_type*, 指向自己
VS的调试也显示
Python源码学习-PyTypeObject_第1张图片

你可能感兴趣的:(Python)