三、对象到底如何创建

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 plain copy
  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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值