一、初识PyObject

来源:互联网 发布:大数据分析系统源代码 编辑:程序博客网 时间:2024/05/17 21:46

PyObject是整个Python对象机制的核心

[object.h]typedef struct _object {PyObject_HEAD} PyObject;
[object.h]#ifdef Py_TRACE_REFS/* Define pointers to support a doubly-linked list of all live heap objects. */#define _PyObject_HEAD_EXTRA            \    struct _object *_ob_next;           \    struct _object *_ob_prev;#define _PyObject_EXTRA_INIT 0, 0,#else#define _PyObject_HEAD_EXTRA#define _PyObject_EXTRA_INIT#endif/* PyObject_HEAD defines the initial segment of every PyObject. */#define PyObject_HEAD                   \    _PyObject_HEAD_EXTRA                \    Py_ssize_t ob_refcnt;               \    struct _typeobject *ob_type;

在实际发布的Python中PyObject的定义非常简单

[object.h]typedef struct _object {    int ob_refcnt;//引用计数    struct _typeobject *ob_type;} PyObject;

这里的ob_refcnt是引用计数。ob_type???让我们看看这个_typeobject,它存储了大量的信息,里面的每一条信息,被称为一个元信息。

[object.h]typedef struct _typeobject {    PyObject_VAR_HEAD    const char *tp_name; /* For printing, in format "." */ 类型名    Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */ 对象分配时内存空间大小    /* Methods to implement standard operations */ 函数指针    destructor tp_dealloc;    printfunc tp_print;    getattrfunc tp_getattr;    setattrfunc tp_setattr;    cmpfunc tp_compare;    reprfunc tp_repr;    /* Method suites for standard classes */  //三组非常重要的操作族    PyNumberMethods *tp_as_number;  //PyNumberMethods类型定义了作为一个数值对象应该支持的操作    PySequenceMethods *tp_as_sequence;    PyMappingMethods *tp_as_mapping;    /* More standard operations (here for binary compatibility) */    hashfunc tp_hash;    ternaryfunc tp_call;    reprfunc tp_str;    getattrofunc tp_getattro;    setattrofunc tp_setattro;    /* Functions to access object as input/output buffer */    PyBufferProcs *tp_as_buffer;    /* Flags to define presence of optional/expanded features */    long tp_flags;    const char *tp_doc; /* Documentation string */    /* Assigned meaning in release 2.0 */    /* call function for all accessible objects */    traverseproc tp_traverse;    /* delete references to contained objects */    inquiry tp_clear;    /* Assigned meaning in release 2.1 */    /* rich comparisons */    richcmpfunc tp_richcompare;    /* weak reference enabler */    Py_ssize_t tp_weaklistoffset;    /* Added in release 2.2 */    /* Iterators */    getiterfunc tp_iter;    iternextfunc tp_iternext;    /* Attribute descriptor and subclassing stuff */    struct PyMethodDef *tp_methods;    struct PyMemberDef *tp_members;    struct PyGetSetDef *tp_getset;    struct _typeobject *tp_base;    PyObject *tp_dict;    descrgetfunc tp_descr_get;    descrsetfunc tp_descr_set;    Py_ssize_t tp_dictoffset;    initproc tp_init;    allocfunc tp_alloc;    newfunc tp_new;    freefunc tp_free; /* Low-level free-memory routine */    inquiry tp_is_gc; /* For PyObject_IS_GC */    PyObject *tp_bases;    PyObject *tp_mro; /* method resolution order */    PyObject *tp_cache;    PyObject *tp_subclasses;    PyObject *tp_weaklist;    destructor tp_del;    /* Type attribute cache version tag. Added in version 2.6 */    unsigned int tp_version_tag;#ifdef COUNT_ALLOCS    /* these must be last and never explicitly initialized */    Py_ssize_t tp_allocs;    Py_ssize_t tp_frees;    Py_ssize_t tp_maxalloc;    struct _typeobject *tp_prev;    struct _typeobject *tp_next;#endif} PyTypeObject;

总结:PyObject里面有一个ob_refcnt(引用计数),一个ob_type(一堆的信息,一堆的函数,一堆这个对象支持的操作)。

原创粉丝点击