# Python 双刃剑

{% hint style="warning" %}
下文中提到的“Python”均特指官方的 CPython 实现
{% endhint %}

## 解释模式

不同于经典 C 的编译模式，Python 是以解释模式运行的。也就是说，只有在实际运行时解释器才会逐行读取对应的代码。

#### 优点

1. 所见即所得，符合直观逻辑（线性顺序执行）。
2. 提升了 DEBUG 效率（支持动态修正）。
3. 简化了开发流程（除去了编译环节）。

#### 缺点

1. 显著降低了运行时效率（无法进行任何的编译时优化）。
2. 增大了制作独立程序包的难度（需要内嵌整个解释器）。

## 动态类型

在 Python 中使用变量不需要任何额外的步骤来声明其类型，变量的实际类型由解释在运行时动态判断且可以发生变动。

#### 优点

1. 简化语法（Pythonic 的核心就是简洁之美）。
2. 显著提升小项目的代码开发效率（无需设计全套的类型接口）。

#### 缺点

1. 影响代码运行的时间和空间效率（需要额外的类型检查；即使是很小的变量也需要存储额外的对象结构数据）。
2. 对于后期维护升级不友好（函数签名包含的信息过少，必须依赖额外文档）。
3. 为 BUG 的存活提供了良好的环境（错误的传参和调用可能不被发现）。

## GIL

Global Interpreter Lock（全局解释器锁）是 CPython 解释器所采用的一种机制，它确保同一时刻只有一个线程在执行 Python bytecode。

#### 优点

1. 使得解释器多线程运行更方便（事实上将底层对象访问转化为了单线程）。

#### 缺点

1. 牺牲了在多处理器上的并行性（一核有难，七核围观）。


---

# 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://blog.wh2099.com/python/python-shuang-ren-jian.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.
