# object.h

## \_PyObject\_HEAD\_EXTRA

```
# 调试的时候定义双链表
#define _PyObject_HEAD_EXTRA            \
    struct _object *_ob_next;           \
    struct _object *_ob_prev;
```

## \_typeobject/PyTypeObject结构体

类型定义结构体

* PyObject\_VAR\_HEAD是可变长对象定义

  `define PyObject_VAR_HEAD PyVarObject ob_base;`
* 创建对象时分配内存空间大小的变量， `tp_basicsize`基本的大小，和`tp_itemsize`里面元素的大小

```c
typedef struct _typeobject {
    PyObject_VAR_HEAD
    const char *tp_name; /* For printing, in format "<module>.<name>" */
    Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */

    destructor tp_dealloc;
    printfunc tp_print;
    getattrfunc tp_getattr;
    setattrfunc tp_setattr;
    PyAsyncMethods *tp_as_async;
    reprfunc tp_repr;
..........
    //各种类型的操作集结构体

    PyNumberMethods *tp_as_number;
    PySequenceMethods *tp_as_sequence;
    PyMappingMethods *tp_as_mapping;
    ........
//  结构体定义指针函数
    hashfunc tp_hash;
    ternaryfunc tp_call;
    reprfunc tp_str;
    getattrofunc tp_getattro;
    setattrofunc tp_setattro;
    inquiry tp_is_gc; /* For PyObject_IS_GC */
} PyTypeObject;
```

## \_object/PyObject结构体

* PyObject包含\_PyObject\_HEAD\_EXTRA头用于调试
* ob\_refcnt引用计数，用于垃圾回收
* ob\_type类型结构体指针，定义类型

```c
# 初级对象 
typedef struct _object {
    _PyObject_HEAD_EXTRA
    Py_ssize_t ob_refcnt;
    struct _typeobject *ob_type;
} PyObject;
```

## PyVarObject结构体

其直接是一个PyObject和一个描述元素数量多int组成

```
// 变长对象
typedef struct {
    PyObject ob_base;
    Py_ssize_t ob_size; /* Number of items in variable part */
} PyVarObject;
```

## 垃圾回收

```
/*
#ifdef Py_REF_DEBUG
765 PyAPI_DATA(Py_ssize_t) _Py_RefTotal;
769 #define _Py_INC_REFTOTAL        _Py_RefTotal++
770 #define _Py_DEC_REFTOTAL        _Py_RefTotal--
771 #define _Py_REF_DEBUG_COMMA     ,
*/

//增加引用计数
#define Py_INCREF(op) (                         \
    ((PyObject *)(op))->ob_refcnt++)


//减少引用计数
#define Py_DECREF(op)                                   \
    do {                                                \
        PyObject *_py_decref_tmp = (PyObject *)(op);    \
        if (--(_py_decref_tmp)->ob_refcnt != 0)         \
            _Py_CHECK_REFCNT(_py_decref_tmp)            \
        else                                            \
            _Py_Dealloc(_py_decref_tmp);                \
    } while (0)

// 销毁函数
PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
```

## PyNumberMethods

对数字类对象的操作结构体

```c
typedef struct {
    /*
    定义了作为一个数值对象支持的操作. 如果一个对象被视为数值对象(如整型), 
    那么它的 tp_as_number.nb_add 就要有对该对象进行加法操作的具体行为
    */

//typedef PyObject * (*binaryfunc)(PyObject *, PyObject *); 
//二元操作，对两个PyObject进行操作，返回一个PyObject
    binaryfunc nb_add;
    binaryfunc nb_subtract;
    binaryfunc nb_multiply;
    binaryfunc nb_remainder;
    binaryfunc nb_divmod;
//  ternaryfunc，三元操作
    ternaryfunc nb_power;
//  unaryfunc，一元操作
    unaryfunc nb_negative;
    unaryfunc nb_positive;
    unaryfunc nb_absolute;
.......
    binaryfunc nb_matrix_multiply;
    binaryfunc nb_inplace_matrix_multiply;
} PyNumberMethods;
```

## PySequenceMethods

序列类型list。。操作

## PyMappingMethods

字典类型dict。。操作

## PyAsyncMethods

异步操作。。。

很多。。。

## 一些宏函数

```c
#define Py_REFCNT(ob)           (((PyObject*)(ob))->ob_refcnt)
#define Py_TYPE(ob)             (((PyObject*)(ob))->ob_type)
#define Py_SIZE(ob)             (((PyVarObject*)(ob))->ob_size)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/python-yuan-ma-yue-du/jilu/include/object-h.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
