# General Translation Python SDKs: extract_variables
URL: https://generaltranslation.com/zh/docs/python/reference/functions/extract-variables.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 从选项字典中过滤掉 GT 保留键，以获取 General Translation Python 中的用户变量。extract_variables 的 API 参考。

返回一个仅包含用户插值变量的新 字典，并移除诸如 `_context` 之类的 General Translation 保留键。这是辅助函数 [`t`](/docs/python/reference/functions/t)、[`msg`](/docs/python/reference/functions/msg) 和 [`interpolate_message`](/docs/python/reference/functions/interpolate-message) 用于分离变量和选项的函数。

## 概述 [#overview]

使用选项字典调用 `extract_variables`。它会返回一个新的字典，包含除 GT 保留键之外的所有键。

```python
from gt_i18n import extract_variables

extract_variables({"name": "Alice", "_context": "greeting"})
# → {"name": "Alice"}
```

签名：

```python
extract_variables(options: dict[str, object]) -> dict[str, object]
```

从 `gt_i18n` 导入 `extract_variables`。框架包不会再次导出它。

## 工作方式 [#how-it-works]

* **保留键。** 会移除 `_context`、`_id`、`_max_chars`、`__hash`、`__source` 和 `__fallback`。
* **其余内容均保留。** 其他所有键都会原样保留，包括以单个下划线开头的用户键 (例如 `_name`) 。
* **不修改原对象。** 它会返回一个新的 `字典`；输入保持不变。

## 参数 [#parameters]

| 参数                    | 描述                    | 类型                  | 可选 | 默认值 |
| --------------------- | --------------------- | ------------------- | -- | --- |
| [`options`](#options) | 完整的选项字典，可能包含 GT 的保留键。 | `dict[str, object]` | 否  | —   |

### `options` [#options]

**类型** `dict[str, object]` · **必填**

完整的选项字典。会移除 GT 保留键，其他所有条目均会保留。

## 返回值 [#returns]

**类型** `dict[str, object]`

一个移除了 GT 保留键的新 字典。

## 示例 [#example]

```python
from gt_i18n import extract_variables

extract_variables({"_context": "x", "_id": "y", "_max_chars": 10})
# → {}

extract_variables({"_name": "Alice", "_context": "greeting"})
# → {"_name": "Alice"}
```

## Sitemap

See the full [sitemap](https://generaltranslation.com/sitemap.md) for all pages.
