三、对象到底如何创建

来源:互联网 发布:80端口提供什么服务 编辑:程序博客网 时间:2024/06/07 22:33

http://blog.csdn.net/debugm/article/details/8241769

一、二 文中知道,无论PyObject还是PyXXXObject,都包含有那么几样东西。

具体举个例子来说,PyIntObject里面有三东西:

int ob_refcnt;//引用计数
struct _typeobject *ob_type; //一堆信息
long ob_ival; //值

只要把这三个要素构成了,不就生成一个基本的"int"了??是不是??

故在intobject.c中有如下代码出现。PyTypeObject就是_typeobject。

[plain] view plaincopy
  1. PyTypeObject PyInt_Type = {  
  2.     PyVarObject_HEAD_INIT(&PyType_Type, 0)  
  3.     "int",  
  4.     sizeof(PyIntObject),  
  5.     0,  
  6.     (destructor)int_dealloc,                    /* tp_dealloc */  
  7.     (printfunc)int_print,                       /* tp_print */  
  8.     0,                                          /* tp_getattr */  
  9.     0,                                          /* tp_setattr */  
  10.     (cmpfunc)int_compare,                       /* tp_compare */  
  11.     (reprfunc)int_to_decimal_string,            /* tp_repr */  
  12.     &int_as_number,                             /* tp_as_number */  
  13.     0,                                          /* tp_as_sequence */  
  14.     0,                                          /* tp_as_mapping */  
  15.     (hashfunc)int_hash,                         /* tp_hash */  
  16.     0,                                          /* tp_call */  
  17.     (reprfunc)int_to_decimal_string,            /* tp_str */  
  18.     PyObject_GenericGetAttr,                    /* tp_getattro */  
  19.     0,                                          /* tp_setattro */  
  20.     0,                                          /* tp_as_buffer */  
  21.     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |  
  22.         Py_TPFLAGS_BASETYPE | Py_TPFLAGS_INT_SUBCLASS,          /* tp_flags */  
  23.     int_doc,                                    /* tp_doc */  
  24.     0,                                          /* tp_traverse */  
  25.     0,                                          /* tp_clear */  
  26.     0,                                          /* tp_richcompare */  
  27.     0,                                          /* tp_weaklistoffset */  
  28.     0,                                          /* tp_iter */  
  29.     0,                                          /* tp_iternext */  
  30.     int_methods,                                /* tp_methods */  
  31.     0,                                          /* tp_members */  
  32.     int_getset,                                 /* tp_getset */  
  33.     0,                                          /* tp_base */  
  34.     0,                                          /* tp_dict */  
  35.     0,                                          /* tp_descr_get */  
  36.     0,                                          /* tp_descr_set */  
  37.     0,                                          /* tp_dictoffset */  
  38.     0,                                          /* tp_init */  
  39.     0,                                          /* tp_alloc */  
  40.     int_new,                                    /* tp_new */  
  41.     (freefunc)int_free,                         /* tp_free */  
  42. };  

总结:在XXXobject.c中,通过声明定义变量PyXXX_Type(PyTypeObject类型的)创建了一个XXX对象。

创建过程会调用一系列函数,具体过程是:tp_new(C++中的new),设置sizeof(PyIntObject),tp_init(视为类的构造函数)

参考:https://github.com/youngsterxyf/Translation/blob/master/Python-object-creation-sequence.rst