From 31d6876e5f869b074149cfed7207ae1887a52211 Mon Sep 17 00:00:00 2001 From: Matt McKay Date: Wed, 25 Mar 2026 03:12:30 +0000 Subject: [PATCH 1/2] Update translation: lectures/python_advanced_features.md --- lectures/python_advanced_features.md | 159 ++++++++++++++++++++++----- 1 file changed, 133 insertions(+), 26 deletions(-) diff --git a/lectures/python_advanced_features.md b/lectures/python_advanced_features.md index b423db6..e451713 100644 --- a/lectures/python_advanced_features.md +++ b/lectures/python_advanced_features.md @@ -8,29 +8,36 @@ kernelspec: language: python name: python3 heading-map: + More Language Features: 更多语言特性 Overview: 概述 - Iterables and Iterators: 可迭代对象与迭代器 - Iterables and Iterators::Iterators: 迭代器 - Iterables and Iterators::Iterators in For Loops: For 循环中的迭代器 - Iterables and Iterators::Iterables: 可迭代对象 - Iterables and Iterators::Iterators and built-ins: 迭代器与内置函数 - '`*` and `**` Operators': '`*` 和 `**` 运算符' - '`*` and `**` Operators::Unpacking Arguments': 解包参数 - '`*` and `**` Operators::Arbitrary Arguments': 任意数量的参数 - Decorators and Descriptors: 装饰器与描述符 - Decorators and Descriptors::Decorators: 装饰器 - Decorators and Descriptors::Decorators::An Example: 一个示例 - Decorators and Descriptors::Decorators::Enter Decorators: 引入装饰器 - Decorators and Descriptors::Descriptors: 描述符 - Decorators and Descriptors::Descriptors::A Solution: 解决方案 - Decorators and Descriptors::Descriptors::How it Works: 工作原理 - Decorators and Descriptors::Descriptors::Decorators and Properties: 装饰器与属性 + Iterables and iterators: 可迭代对象与迭代器 + Iterables and iterators::Iterators: 迭代器 + Iterables and iterators::Iterators in for loops: for 循环中的迭代器 + Iterables and iterators::Iterables: 可迭代对象 + Iterables and iterators::Iterators and built-ins: 迭代器与内置函数 + '`*` and `**` operators': '`*` 和 `**` 运算符' + '`*` and `**` operators::Unpacking arguments': 解包参数 + '`*` and `**` operators::Arbitrary arguments': 任意数量的参数 + Type hints: 类型提示 + Type hints::Basic syntax: 基本语法 + Type hints::Common types: 常用类型 + Type hints::Hints don't enforce types: 提示不强制类型 + Type hints::Why use type hints?: 为什么使用类型提示? + Type hints::Type hints in scientific Python: 科学 Python 中的类型提示 + Decorators and descriptors: 装饰器与描述符 + Decorators and descriptors::Decorators: 装饰器 + Decorators and descriptors::Decorators::An example: 一个示例 + Decorators and descriptors::Decorators::Enter decorators: 引入装饰器 + Decorators and descriptors::Descriptors: 描述符 + Decorators and descriptors::Descriptors::A solution: 解决方案 + Decorators and descriptors::Descriptors::How it works: 工作原理 + Decorators and descriptors::Descriptors::Decorators and properties: 装饰器与属性 Generators: 生成器 - Generators::Generator Expressions: 生成器表达式 - Generators::Generator Functions: 生成器函数 - Generators::Generator Functions::Example 1: 示例 1 - Generators::Generator Functions::Example 2: 示例 2 - Generators::Advantages of Iterators: 迭代器的优势 + Generators::Generator expressions: 生成器表达式 + Generators::Generator functions: 生成器函数 + Generators::Generator functions::Example 1: 示例 1 + Generators::Generator functions::Example 2: 示例 2 + Generators::Advantages of iterators: 迭代器的优势 Exercises: 练习 --- @@ -47,14 +54,14 @@ heading-map: ## 概述 -关于这最后一讲,我们的建议是**第一次阅读时跳过它**,除非你迫切想要阅读。 +关于这最后一讲,我们的建议是*第一次阅读时跳过它*,除非你迫切想要阅读。 它在这里有两个用途: 1. 作为参考,以便我们在需要时可以链接回来,以及 1. 供那些已经完成了许多应用练习、现在想要深入了解 Python 语言的人使用。 -本讲涵盖了多种主题,包括迭代器、装饰器和描述符,以及生成器。 +本讲涵盖了多种主题,包括迭代器、类型提示、装饰器和描述符,以及生成器。 ## 可迭代对象与迭代器 @@ -155,7 +162,7 @@ next(nikkei_data) next(nikkei_data) ``` -### For 循环中的迭代器 +### for 循环中的迭代器 ```{index} single: Python; Iterators ``` @@ -486,6 +493,107 @@ arb(l1=l1, l2=l2, l3=l3) 区别在于带有 `*args` 的函数能够接受任意大小的*位置参数*,而 `**kargs` 允许函数接受任意数量的*关键字参数*。 +## 类型提示 + +```{index} single: Python; Type Hints +``` + +Python 是一种*动态类型*语言,这意味着你无需声明变量的类型。 + +(请参阅我们 {doc}`之前的讨论 `,了解动态类型与静态类型的区别。) + +然而,Python 支持可选的**类型提示**(也称为类型注解),允许你指定变量、函数参数和返回值的预期类型。 + +类型提示从 Python 3.5 开始引入,并在后续版本中不断演进。此处展示的所有语法均适用于 Python 3.9 及更高版本。 + +```{note} +类型提示在*运行时会被 Python 解释器忽略*——它们不会影响代码的执行方式。它们纯粹是信息性的,作为面向人类和工具的文档。 +``` + +### 基本语法 + +类型提示使用冒号 `:` 对变量和参数进行注解,使用箭头 `->` 对返回类型进行注解。 + +以下是一个简单示例: + +```{code-cell} python3 +def greet(name: str, times: int) -> str: + return (name + '! ') * times + +greet('hello', 3) +``` + +在这个函数定义中: + +- `name: str` 表示 `name` 预期为字符串类型 +- `times: int` 表示 `times` 预期为整数类型 +- `-> str` 表示函数返回一个字符串 + +你也可以直接对变量进行注解: + +```{code-cell} python3 +x: int = 10 +y: float = 3.14 +name: str = 'Python' +``` + +### 常用类型 + +最常用的类型提示是内置类型: + +| 类型 | 示例 | +|-----------|----------------------------------| +| `int` | `x: int = 5` | +| `float` | `x: float = 3.14` | +| `str` | `x: str = 'hello'` | +| `bool` | `x: bool = True` | +| `list` | `x: list = [1, 2, 3]` | +| `dict` | `x: dict = {'a': 1}` | + +对于容器类型,你可以指定其元素的类型: + +```{code-cell} python3 +prices: list[float] = [9.99, 4.50, 2.89] +counts: dict[str, int] = {'apples': 3, 'oranges': 5} +``` + +### 提示不强制类型 + +对于 Python 初学者来说有一点很重要:类型提示在运行时*不会被强制执行*。 + +如果你传入了"错误"的类型,Python 不会抛出错误: + +```{code-cell} python3 +def add(x: int, y: int) -> int: + return x + y + +# Passes floats — Python doesn't complain +add(1.5, 2.7) +``` + +提示说的是 `int`,但 Python 愉快地接受了 `float` 参数并返回 `4.2`——同样不是 `int`。 + +这是与 C 或 Java 等静态类型语言的关键区别,在那些语言中,类型不匹配会导致编译错误。 + +### 为什么使用类型提示? + +既然 Python 会忽略它们,为什么还要使用? + +1. **可读性**:类型提示使函数签名具有自文档化效果。读者可以立即了解函数期望的输入类型和返回类型。 +2. **编辑器支持**:VS Code 等 IDE 利用类型提示提供更好的自动补全、错误检测和内联文档功能。 +3. **错误检查**:[mypy](https://mypy.readthedocs.io/) 和 [pyrefly](https://pyrefly.org/) 等工具通过分析类型提示,在*运行代码之前*就能发现错误。 +4. **LLM 生成的代码**:大型语言模型经常生成带有类型提示的代码,因此理解其语法有助于阅读和使用它们的输出。 + +### 科学 Python 中的类型提示 + +类型提示与 {doc}`性能需求 ` 的讨论密切相关: + +* [JAX](https://jax.readthedocs.io/) 和 [Numba](https://numba.pydata.org/) 等高性能库依赖于了解变量类型来编译快速的机器码。 +* 虽然这些库在运行时推断类型,而不是直接读取 Python 类型提示,但*概念*是相同的——显式的类型信息能够实现优化。 +* 随着 Python 生态系统的发展,类型提示与性能工具之间的联系预计将进一步加强。 + +目前,类型提示在日常 Python 开发中的主要优势在于*清晰性和工具支持*,随着程序规模的增大,这一优势的价值也愈发显著。 + ## 装饰器与描述符 ```{index} single: Python; Decorators @@ -1079,7 +1187,6 @@ sum(draws) * 避免了创建大型列表/元组的需要,以及 * 提供了一个统一的迭代接口,可以在 `for` 循环中透明地使用。 - ## 练习 @@ -1140,4 +1247,4 @@ for date in dates: ``` ```{solution-end} -``` \ No newline at end of file +``` From 6dbaacd8d7bd34fa98c8a515d4ae490d986f6dea Mon Sep 17 00:00:00 2001 From: Matt McKay Date: Wed, 25 Mar 2026 03:12:30 +0000 Subject: [PATCH 2/2] Update translation: .translate/state/python_advanced_features.md.yml --- .translate/state/python_advanced_features.md.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.translate/state/python_advanced_features.md.yml b/.translate/state/python_advanced_features.md.yml index 32c8e5f..e35bd4a 100644 --- a/.translate/state/python_advanced_features.md.yml +++ b/.translate/state/python_advanced_features.md.yml @@ -1,6 +1,6 @@ -source-sha: 1a87942398e15e03539083cc944a78653c532607 -synced-at: "2026-03-20" +source-sha: f791129259d11c138d7662c88f994c9d5ebaa875 +synced-at: "2026-03-25" model: claude-sonnet-4-6 -mode: NEW -section-count: 6 -tool-version: 0.11.1 +mode: UPDATE +section-count: 7 +tool-version: 0.12.4