Python对象初探

数据结构

PyObject_HEAD //对象公共头部

 

Py_ssize_t ob_refcnt;  //对象引用数

PyTypeObject *ob_type; //对象类型

PyObject_VAR_HEAD //这个就是属于可变对象头部啦

PyObject_HEAD  //对象头部

Py_ssize_t ob_size;//元素个数

展开了就是

PyObject_VAR_HEAD = {

Py_ssize_t ob_refcnt;  //对象引用数

PyTypeObject *ob_type; //对象类型

Py_ssize_t ob_size;//元素个数

}

PyObject_HEAD_INIT(type) //不可变对象初始化

This is a macro which expands to initialization values for a new PyObject type. This macro expands to:

_PyObject_EXTRA_INIT

1, type,

//头部初始化,对象引入数初始化为1,对象类型为传入来的类型对象

PyVarObject_HEAD_INIT(type, size)//可变对象初始化

This is a macro which expands to initialization values for a new PyVarObject type, including the ob_size field. This macro expands to:

_PyObject_EXTRA_INIT

1, type, size,

//头部初始化,对象引入数初始化为1,对象类型为传入来的类型对象,大小为传进来的大小

而上面都有一个_PyObject_EXTRA_INIT

这个是因为系统定义了
							Py_TRACE_REFS
					

所有要初始化 next和prev这两个指针

/* PyObject_HEAD defines the initial segment of every PyObject. */
					

#define PyObject_HEAD           /
					

    _PyObject_HEAD_EXTRA        /

    int
					ob_refcnt;          /

    struct
					_typeobject
					*ob_type;
					

   

#define PyObject_VAR_HEAD       /
					

    PyObject_HEAD           /

    int
					ob_size;
					/* Number of items in variable part */
					

 

不变对象定义

typedef struct _object {

    PyObject_HEAD

} PyObject;

 

可变对象定义

typedef
						struct
					{
					

    PyObject_VAR_HEAD
					

}
					PyVarObject;
					

看书一定要仔细啊

类型对象的结构

[object.h]

typedef struct _typeobject {

    PyObject_VAR_HEAD

    char *tp_name; /* For printing, in format "<module>.<name>" */

    int tp_basicsize, tp_itemsize; /* For allocation */

   

    /* Methods to implement standard operations */

    destructor tp_dealloc;

printfunc tp_print;

……

    /* More standard operations (here for binary compatibility) */

    hashfunc tp_hash;

    ternaryfunc tp_call;

    ……

} PyTypeObject;

_typeobject的定义中包含了许多的信息,主要可以分为四类:

1.类型名,tp_name,主要是Python内部以及调试的时候使用;

2.创建该类型对象是分配内存空间的大小的信息,即tp_basicsizetp_itemsize

3.与该类型对象相关联的操作信息,比如hashfunctp_hash就指明对于该类型的对象,如何生成其hash值。在Object.h中可以看到,hashfunc实际上是一个函数指针:typedef long (*hashfunc)(PyObject *); _typeobject中,包含了大量的函数指针,这些函数指针将用来指定某个类型的操作信息。这些操作主要分为标准操作(dealloc, print, compare),标准操作族(numbers, sequences, mappings),以及其他操作(hash, buffer, call…)。

接下来我们看看整形是如何初始化的

[intobject.c]

PyTypeObject PyInt_Type = {

    PyObject_HEAD_INIT(&PyType_Type)

    0,

    "int",

    sizeof(PyIntObject),

    ……

};

PyType_Type这个又是什么东西啊

PyObject_HEAD_INIT 这个宏定义如下这样的,

 

PyType_Type 这个的数据结构是这样的,其实我觉得它是PyTypeObject的一个实例

 

[typeobject.c]

PyTypeObject PyType_Type = {

PyObject_HEAD_INIT(&PyType_Type)

/*

上面宏展开就是

1 //引用计数

&PyType_Type

*/

    0,                  /* ob_size 元素个数*/

    "type",                 /* tp_name */

    sizeof(PyHeapTypeObject),       /* tp_basicsize */

    sizeof(PyMemberDef),            /* tp_itemsize */

    ……

    PyObject_GC_Del,                /* tp_free */

    (inquiry)type_is_gc,            /* tp_is_gc */

};

还是看源码好过

[intobject.h]

typedef
						struct
					{
				

PyObject_HEAD

/* 这里宏展开就是
					

    Py_ssize_t ob_refcnt;  //对象引用数

PyTypeObject *ob_type; //对象类型
				

*/
					

    long
					ob_ival;
					

}
				PyIntObject;
					

 

 

 

 

 

PyTypeObject
					PyInt_Type
					=
					{
					

PyObject_HEAD_INIT(&PyType_Type)

/*

这里宏展开就是
					

1 //引用计数
					

&PyType_Type //类型对象
						

*/

 

    0, //元素个数
						

    "int",

    sizeof(PyIntObject),

    0,

    (destructor)int_dealloc,        /* tp_dealloc */
					

    (printfunc)int_print,           /* tp_print */
					

    0,                  /* tp_getattr */
					

    0,                  /* tp_setattr */
					

    (cmpfunc)int_compare,           /* tp_compare */
					

    (reprfunc)int_repr,         /* tp_repr */
					

    &int_as_number,             /* tp_as_number */
					

    0,                  /* tp_as_sequence */
					

    0,                  /* tp_as_mapping */
					

    (hashfunc)int_hash,         /* tp_hash */
					

    0,                  /* tp_call */
					

    (reprfunc)int_repr,         /* tp_str */
					

    PyObject_GenericGetAttr,        /* tp_getattro */
					

    0,                  /* tp_setattro */
					

    0,                  /* tp_as_buffer */
					

    Py_TPFLAGS_DEFAULT
					|
					Py_TPFLAGS_CHECKTYPES
					|
					Py_TPFLAGS_BASETYPE, /* tp_flags */
					

    int_doc,                /* tp_doc */
					

    0,                  /* tp_traverse */
					

    0,                  /* tp_clear */
					

    0,                  /* tp_richcompare */
					

    0,                  /* tp_weaklistoffset */
					

    0,                  /* tp_iter */
					

    0,                  /* tp_iternext */
					

    int_methods,        /* tp_methods */
					

    0,                  /* tp_members */
					

    0,                  /* tp_getset */
					

    0,                  /* tp_base */
					

    0,                  /* tp_dict */
					

    0,                  /* tp_descr_get */
					

    0,                  /* tp_descr_set */
					

    0,                  /* tp_dictoffset */
					

    0,                  /* tp_init */
					

    0,                  /* tp_alloc */
					

    int_new,                /* tp_new */
					

    (freefunc)int_free,                 /* tp_free */
					

};
					

到这里我们还没发现PyType_Type 这个东西有什么用

打印的方法 根本就没有用到那个属性

 

 

这个图还不错

 

转载于:https://www.cnblogs.com/simonlu/p/3621333.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值