> For the complete documentation index, see [llms.txt](https://hai-shi.gitbook.io/cpython-internals/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hai-shi.gitbook.io/cpython-internals/12-objects-types/12.3-object-and-variable-object-types.md).

# 12.3 对象和可变长度对象类型

C 语言不是像 [Python](https://realpython.com/python3-object-oriented-programming/) 这样的面向对象语言，C 语言中的对象不会继承其他对象。`PyObject` 是每个 Python 对象的初始数据段，`PyObject *` 表示指向 Python 对象的指针。

当定义 Python 类型时，`typedef` 使用如下两个宏中的一个：

* `PyObject_HEAD (PyObject)` 用于简单的类型
* `PyObject_VAR_HEAD (PyVarObject)` 用于容器类型。

`PyObject` 比较简单，它有如下字段：

| 字段          | 类型             | 用途       |
| ----------- | -------------- | -------- |
| `ob_refcnt` | `Py_ssize_t`   | 实例的引用计数器 |
| `ob_type`   | `_typeobject*` | 对象的类型    |

例如，`cellobject` 除了基础字段外，声明了一个额外的字段 `ob_ref`：

```c
typedef struct {
    PyObject_HEAD
    PyObject *ob_ref; /* Content of the cell or NULL when empty */
} PyCellObject;
```

可变长度类型 `PyVarObject` 扩展了 `PyObject` 类型，有如下字段：

| 字段        | 类型           | 用途      |
| --------- | ------------ | ------- |
| `ob_base` | `PyObject`   | 基类型     |
| `ob_size` | `Py_ssize_t` | 包含的项目数量 |

例如，`int` 类型（`PyLongObject`），定义如下：

```c
struct _longobject {
    PyObject_VAR_HEAD
    digit ob_digit[1];
}; /* PyLongObject */
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://hai-shi.gitbook.io/cpython-internals/12-objects-types/12.3-object-and-variable-object-types.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
