Skip to content

Commit 71d9288

Browse files
authored
🌐 [translation-sync] Add Type Hints section to More Language Features lecture (#11)
* Update translation: lectures/python_advanced_features.md * Update translation: .translate/state/python_advanced_features.md.yml
1 parent 4bce417 commit 71d9288

File tree

2 files changed

+138
-31
lines changed

2 files changed

+138
-31
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
source-sha: 1a87942398e15e03539083cc944a78653c532607
2-
synced-at: "2026-03-20"
1+
source-sha: f791129259d11c138d7662c88f994c9d5ebaa875
2+
synced-at: "2026-03-25"
33
model: claude-sonnet-4-6
4-
mode: NEW
5-
section-count: 6
6-
tool-version: 0.11.1
4+
mode: UPDATE
5+
section-count: 7
6+
tool-version: 0.12.4

lectures/python_advanced_features.md

Lines changed: 133 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,36 @@ kernelspec:
88
language: python
99
name: python3
1010
heading-map:
11+
More Language Features: 更多语言特性
1112
Overview: 概述
12-
Iterables and Iterators: 可迭代对象与迭代器
13-
Iterables and Iterators::Iterators: 迭代器
14-
Iterables and Iterators::Iterators in For Loops: For 循环中的迭代器
15-
Iterables and Iterators::Iterables: 可迭代对象
16-
Iterables and Iterators::Iterators and built-ins: 迭代器与内置函数
17-
'`*` and `**` Operators': '`*` 和 `**` 运算符'
18-
'`*` and `**` Operators::Unpacking Arguments': 解包参数
19-
'`*` and `**` Operators::Arbitrary Arguments': 任意数量的参数
20-
Decorators and Descriptors: 装饰器与描述符
21-
Decorators and Descriptors::Decorators: 装饰器
22-
Decorators and Descriptors::Decorators::An Example: 一个示例
23-
Decorators and Descriptors::Decorators::Enter Decorators: 引入装饰器
24-
Decorators and Descriptors::Descriptors: 描述符
25-
Decorators and Descriptors::Descriptors::A Solution: 解决方案
26-
Decorators and Descriptors::Descriptors::How it Works: 工作原理
27-
Decorators and Descriptors::Descriptors::Decorators and Properties: 装饰器与属性
13+
Iterables and iterators: 可迭代对象与迭代器
14+
Iterables and iterators::Iterators: 迭代器
15+
Iterables and iterators::Iterators in for loops: for 循环中的迭代器
16+
Iterables and iterators::Iterables: 可迭代对象
17+
Iterables and iterators::Iterators and built-ins: 迭代器与内置函数
18+
'`*` and `**` operators': '`*` 和 `**` 运算符'
19+
'`*` and `**` operators::Unpacking arguments': 解包参数
20+
'`*` and `**` operators::Arbitrary arguments': 任意数量的参数
21+
Type hints: 类型提示
22+
Type hints::Basic syntax: 基本语法
23+
Type hints::Common types: 常用类型
24+
Type hints::Hints don't enforce types: 提示不强制类型
25+
Type hints::Why use type hints?: 为什么使用类型提示?
26+
Type hints::Type hints in scientific Python: 科学 Python 中的类型提示
27+
Decorators and descriptors: 装饰器与描述符
28+
Decorators and descriptors::Decorators: 装饰器
29+
Decorators and descriptors::Decorators::An example: 一个示例
30+
Decorators and descriptors::Decorators::Enter decorators: 引入装饰器
31+
Decorators and descriptors::Descriptors: 描述符
32+
Decorators and descriptors::Descriptors::A solution: 解决方案
33+
Decorators and descriptors::Descriptors::How it works: 工作原理
34+
Decorators and descriptors::Descriptors::Decorators and properties: 装饰器与属性
2835
Generators: 生成器
29-
Generators::Generator Expressions: 生成器表达式
30-
Generators::Generator Functions: 生成器函数
31-
Generators::Generator Functions::Example 1: 示例 1
32-
Generators::Generator Functions::Example 2: 示例 2
33-
Generators::Advantages of Iterators: 迭代器的优势
36+
Generators::Generator expressions: 生成器表达式
37+
Generators::Generator functions: 生成器函数
38+
Generators::Generator functions::Example 1: 示例 1
39+
Generators::Generator functions::Example 2: 示例 2
40+
Generators::Advantages of iterators: 迭代器的优势
3441
Exercises: 练习
3542
---
3643

@@ -47,14 +54,14 @@ heading-map:
4754

4855
## 概述
4956

50-
关于这最后一讲,我们的建议是**第一次阅读时跳过它**,除非你迫切想要阅读。
57+
关于这最后一讲,我们的建议是*第一次阅读时跳过它*,除非你迫切想要阅读。
5158

5259
它在这里有两个用途:
5360

5461
1. 作为参考,以便我们在需要时可以链接回来,以及
5562
1. 供那些已经完成了许多应用练习、现在想要深入了解 Python 语言的人使用。
5663

57-
本讲涵盖了多种主题,包括迭代器、装饰器和描述符,以及生成器。
64+
本讲涵盖了多种主题,包括迭代器、类型提示、装饰器和描述符,以及生成器。
5865

5966
## 可迭代对象与迭代器
6067

@@ -155,7 +162,7 @@ next(nikkei_data)
155162
next(nikkei_data)
156163
```
157164

158-
### For 循环中的迭代器
165+
### for 循环中的迭代器
159166

160167
```{index} single: Python; Iterators
161168
```
@@ -486,6 +493,107 @@ arb(l1=l1, l2=l2, l3=l3)
486493

487494
区别在于带有 `*args` 的函数能够接受任意大小的*位置参数*,而 `**kargs` 允许函数接受任意数量的*关键字参数*
488495

496+
## 类型提示
497+
498+
```{index} single: Python; Type Hints
499+
```
500+
501+
Python 是一种*动态类型*语言,这意味着你无需声明变量的类型。
502+
503+
(请参阅我们 {doc}`之前的讨论 <need_for_speed>`,了解动态类型与静态类型的区别。)
504+
505+
然而,Python 支持可选的**类型提示**(也称为类型注解),允许你指定变量、函数参数和返回值的预期类型。
506+
507+
类型提示从 Python 3.5 开始引入,并在后续版本中不断演进。此处展示的所有语法均适用于 Python 3.9 及更高版本。
508+
509+
```{note}
510+
类型提示在*运行时会被 Python 解释器忽略*——它们不会影响代码的执行方式。它们纯粹是信息性的,作为面向人类和工具的文档。
511+
```
512+
513+
### 基本语法
514+
515+
类型提示使用冒号 `:` 对变量和参数进行注解,使用箭头 `->` 对返回类型进行注解。
516+
517+
以下是一个简单示例:
518+
519+
```{code-cell} python3
520+
def greet(name: str, times: int) -> str:
521+
return (name + '! ') * times
522+
523+
greet('hello', 3)
524+
```
525+
526+
在这个函数定义中:
527+
528+
- `name: str` 表示 `name` 预期为字符串类型
529+
- `times: int` 表示 `times` 预期为整数类型
530+
- `-> str` 表示函数返回一个字符串
531+
532+
你也可以直接对变量进行注解:
533+
534+
```{code-cell} python3
535+
x: int = 10
536+
y: float = 3.14
537+
name: str = 'Python'
538+
```
539+
540+
### 常用类型
541+
542+
最常用的类型提示是内置类型:
543+
544+
| 类型 | 示例 |
545+
|-----------|----------------------------------|
546+
| `int` | `x: int = 5` |
547+
| `float` | `x: float = 3.14` |
548+
| `str` | `x: str = 'hello'` |
549+
| `bool` | `x: bool = True` |
550+
| `list` | `x: list = [1, 2, 3]` |
551+
| `dict` | `x: dict = {'a': 1}` |
552+
553+
对于容器类型,你可以指定其元素的类型:
554+
555+
```{code-cell} python3
556+
prices: list[float] = [9.99, 4.50, 2.89]
557+
counts: dict[str, int] = {'apples': 3, 'oranges': 5}
558+
```
559+
560+
### 提示不强制类型
561+
562+
对于 Python 初学者来说有一点很重要:类型提示在运行时*不会被强制执行*
563+
564+
如果你传入了"错误"的类型,Python 不会抛出错误:
565+
566+
```{code-cell} python3
567+
def add(x: int, y: int) -> int:
568+
return x + y
569+
570+
# Passes floats — Python doesn't complain
571+
add(1.5, 2.7)
572+
```
573+
574+
提示说的是 `int`,但 Python 愉快地接受了 `float` 参数并返回 `4.2`——同样不是 `int`
575+
576+
这是与 C 或 Java 等静态类型语言的关键区别,在那些语言中,类型不匹配会导致编译错误。
577+
578+
### 为什么使用类型提示?
579+
580+
既然 Python 会忽略它们,为什么还要使用?
581+
582+
1. **可读性**:类型提示使函数签名具有自文档化效果。读者可以立即了解函数期望的输入类型和返回类型。
583+
2. **编辑器支持**:VS Code 等 IDE 利用类型提示提供更好的自动补全、错误检测和内联文档功能。
584+
3. **错误检查**[mypy](https://mypy.readthedocs.io/)[pyrefly](https://pyrefly.org/) 等工具通过分析类型提示,在*运行代码之前*就能发现错误。
585+
4. **LLM 生成的代码**:大型语言模型经常生成带有类型提示的代码,因此理解其语法有助于阅读和使用它们的输出。
586+
587+
### 科学 Python 中的类型提示
588+
589+
类型提示与 {doc}`性能需求 <need_for_speed>` 的讨论密切相关:
590+
591+
* [JAX](https://jax.readthedocs.io/)[Numba](https://numba.pydata.org/) 等高性能库依赖于了解变量类型来编译快速的机器码。
592+
* 虽然这些库在运行时推断类型,而不是直接读取 Python 类型提示,但*概念*是相同的——显式的类型信息能够实现优化。
593+
* 随着 Python 生态系统的发展,类型提示与性能工具之间的联系预计将进一步加强。
594+
595+
目前,类型提示在日常 Python 开发中的主要优势在于*清晰性和工具支持*,随着程序规模的增大,这一优势的价值也愈发显著。
596+
489597
## 装饰器与描述符
490598

491599
```{index} single: Python; Decorators
@@ -1079,7 +1187,6 @@ sum(draws)
10791187
* 避免了创建大型列表/元组的需要,以及
10801188
* 提供了一个统一的迭代接口,可以在 `for` 循环中透明地使用。
10811189

1082-
10831190
## 练习
10841191

10851192

@@ -1140,4 +1247,4 @@ for date in dates:
11401247
```
11411248

11421249
```{solution-end}
1143-
```
1250+
```

0 commit comments

Comments
 (0)