Object对象

文件位置:

  1. \cpython-main\include\object.h
  2. \cpython-main\Objects\object.c

以下是出自源码中的注释:

1、对象是分配在堆上的结构。对象的使用适用特殊规则,以确保它们被正确地垃圾回收。对象永远不会静态分配或在栈上分配;只能通过特殊的宏和函数访问它们。Type对象是这条规则的例外;标准类型由静态初始化的类型对象表示,尽管Python 2.2的类型/类统一工作也使堆分配类型对象成为可能。

2、对象的“引用计数”在复制或删除指向该对象的指针时增加或减少;当引用计数当达到0时,就没有对该对象的引用了,它可以从堆中移除。

3、一个对象中有Type字段用来表示它代表什么挥着包含什么样的数据类型。type在创建时是固定的。type本身也是对象,所以type对象中包含一个指向自己的指针。

4、对象不会再内存中浮动。一旦被分配对象,它将保持相同的大小和地址。用于保存可变大小数据的对象可以包含一个指向一个可变大小部分的指针。并不是所有相同type的对象都有相同的大小,但是一旦被分配就不能再改变大小。这些限制使得对对象的引用可以仅仅是一个简单的指针,移动位置可能导致要更新所有的指针,而改变大小则需要移动它。

5、对象总是通过类型为“PyObject *”的指针访问。而PyObject知识一个包含应用计数器和类型的数据结构

PyObject定义

  1. typedef struct _object {
  2. _PyObject_HEAD_EXTRA
  3. Py_ssize_t ob_refcnt;
  4. PyTypeObject *ob_type;
  5. } PyObject;

PyVarObject定义,对于大小可变对象的定义

  1. typedef struct {
  2. PyObject ob_base;
  3. Py_ssize_t ob_size; /* Number of items in variable part */
  4. } PyVarObject;

type对象定义于 _typeobject这一结构体中,等价于 PyTypeObject。【一些简单的变量解释本文写在了注释里,比较复杂会在后面解释】

  1. struct _typeobject {
  2. PyObject_VAR_HEAD //#define PyObject_VAR_HEAD PyVarObject ob_base;
  3. const char *tp_name; /* For printing, in format "<module>.<name>" */
  4. Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
  5. /* Methods to implement standard operations */
  6. destructor tp_dealloc;
  7. Py_ssize_t tp_vectorcall_offset;
  8. getattrfunc tp_getattr;
  9. setattrfunc tp_setattr;
  10. PyAsyncMethods *tp_as_async; /* formerly known as tp_compare (Python 2)
  11. or tp_reserved (Python 3) */
  12. reprfunc tp_repr;//repr() 函数将对象转化为供解释器读取的形式。调__repr__(self)
  13. /* Method suites for standard classes */
  14. PyNumberMethods *tp_as_number;
  15. PySequenceMethods *tp_as_sequence;
  16. PyMappingMethods *tp_as_mapping;
  17. /* More standard operations (here for binary compatibility) */
  18. hashfunc tp_hash;
  19. ternaryfunc tp_call;
  20. reprfunc tp_str;
  21. getattrofunc tp_getattro;
  22. setattrofunc tp_setattro;
  23. /* Functions to access object as input/output buffer */
  24. PyBufferProcs *tp_as_buffer;
  25. /* Flags to define presence of optional/expanded features */
  26. unsigned long tp_flags;
  27. const char *tp_doc; /* Documentation string */
  28. /* Assigned meaning in release 2.0 */
  29. /* call function for all accessible objects */
  30. traverseproc tp_traverse;
  31. /* delete references to contained objects */
  32. inquiry tp_clear;
  33. /* Assigned meaning in release 2.1 */
  34. /* rich comparisons */
  35. richcmpfunc tp_richcompare;
  36. /* weak reference enabler */
  37. Py_ssize_t tp_weaklistoffset;
  38. /* Iterators */
  39. getiterfunc tp_iter;
  40. iternextfunc tp_iternext;
  41. /* Attribute descriptor and subclassing stuff */
  42. struct PyMethodDef *tp_methods;
  43. struct PyMemberDef *tp_members;
  44. struct PyGetSetDef *tp_getset;
  45. // Strong reference on a heap type, borrowed reference on a static type
  46. struct _typeobject *tp_base;
  47. PyObject *tp_dict;
  48. descrgetfunc tp_descr_get;
  49. descrsetfunc tp_descr_set;
  50. Py_ssize_t tp_dictoffset;
  51. initproc tp_init;
  52. allocfunc tp_alloc;
  53. newfunc tp_new;
  54. freefunc tp_free; /* Low-level free-memory routine */
  55. inquiry tp_is_gc; /* For PyObject_IS_GC */
  56. PyObject *tp_bases;
  57. PyObject *tp_mro; /* method resolution order */
  58. PyObject *tp_cache;
  59. PyObject *tp_subclasses;
  60. PyObject *tp_weaklist;
  61. destructor tp_del;
  62. /* Type attribute cache version tag. Added in version 2.6 */
  63. unsigned int tp_version_tag;
  64. destructor tp_finalize;
  65. vectorcallfunc tp_vectorcall;
  66. };

type中有很多函数指针:

  • destructortypedef void (*destructor)(PyObject *) 用于回收内存

PyNumberMethods是定义为如下的结构体:其中 binaryfunc是指所有有两个参数的函数指针, ternaryfunc是有三个参数的函数指针。具体定义如下:

typedef PyObject * (*unaryfunc)(PyObject *);

typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);

typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);

typedef int (*inquiry)(PyObject *);

  1. typedef struct {
  2. /* Number implementations must check *both*
  3. arguments for proper type and implement the necessary conversions
  4. in the slot functions themselves. */
  5. binaryfunc nb_add;
  6. binaryfunc nb_subtract;
  7. binaryfunc nb_multiply;
  8. binaryfunc nb_remainder;
  9. binaryfunc nb_divmod;
  10. ternaryfunc nb_power;
  11. unaryfunc nb_negative;
  12. unaryfunc nb_positive;
  13. unaryfunc nb_absolute;
  14. inquiry nb_bool;
  15. unaryfunc nb_invert;
  16. binaryfunc nb_lshift;
  17. binaryfunc nb_rshift;
  18. binaryfunc nb_and;
  19. binaryfunc nb_xor;
  20. binaryfunc nb_or;
  21. unaryfunc nb_int;
  22. void *nb_reserved; /* the slot formerly known as nb_long */
  23. unaryfunc nb_float;
  24. binaryfunc nb_inplace_add;//+=
  25. binaryfunc nb_inplace_subtract;//-+
  26. binaryfunc nb_inplace_multiply;//*=
  27. binaryfunc nb_inplace_remainder;
  28. ternaryfunc nb_inplace_power;
  29. binaryfunc nb_inplace_lshift;
  30. binaryfunc nb_inplace_rshift;
  31. binaryfunc nb_inplace_and;
  32. binaryfunc nb_inplace_xor;
  33. binaryfunc nb_inplace_or;
  34. binaryfunc nb_floor_divide;
  35. binaryfunc nb_true_divide;
  36. binaryfunc nb_inplace_floor_divide;
  37. binaryfunc nb_inplace_true_divide;
  38. unaryfunc nb_index;
  39. binaryfunc nb_matrix_multiply;
  40. binaryfunc nb_inplace_matrix_multiply;
  41. } PyNumberMethods;

PySequenceMethods是关于顺序对象的操作,结构定义如下:

  1. typedef struct {
  2. lenfunc sq_length;
  3. binaryfunc sq_concat;
  4. ssizeargfunc sq_repeat;
  5. ssizeargfunc sq_item;
  6. void *was_sq_slice;
  7. ssizeobjargproc sq_ass_item;
  8. void *was_sq_ass_slice;
  9. objobjproc sq_contains;
  10. binaryfunc sq_inplace_concat;
  11. ssizeargfunc sq_inplace_repeat;
  12. } PySequenceMethods;

PySequenceMethods中函数指针的意义与之前类似,都是使用typedef定义的指针,lenfunc即返回为数值的函数:typedef Py_ssize_t (*lenfunc)(PyObject *);,ssizeargfunc为typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);表示有一个参数传入,ssizessizeargfunc表示有两个参数传入。之后类似的函数指针定义不在赘述,可以根据名称自行猜测。