# 本文总结了 Python 3.5 到 3.14 各版本的主要语言特性和改进。

本文总结了 Python 3.5 到 3.14 各版本的主要语言特性和改进。

Python 3.5 (2015年9月)

主要特性

  1. 类型提示 (Type Hints)

    python 复制代码
    def greeting(name: str) -> str:
        return 'Hello ' + name
  2. PEP 492 - 异步 IO 支持

    • asyncawait 关键字
    • 异步生成器和协程
  3. PEP 448 - 扩展的可迭代解包

    python 复制代码
    a, *b, c = [1, 2, 3, 4, 5]  # a=1, b=[2,3,4], c=5
  4. 新增标准库模块

    • typing: 提供类型提示支持
    • asyncio: 异步 IO 框架
  5. 其他改进

    • @ 运算符用于矩阵乘法
    • os.scandir() 用于高效目录遍历

Python 3.6 (2016年12月)

主要特性

  1. 格式化字符串字面值 (f-strings)

    python 复制代码
    name = "World"
    print(f"Hello, {name}!")
  2. PEP 526 - 变量注解

    python 复制代码
    x: int = 10
    y: List[str] = []
  3. PEP 515 - 数字字面值的下划线分隔符

    python 复制代码
    large_number = 1_000_000_000
  4. 异步生成器和异步推导式

    python 复制代码
    async def async_gen():
        for i in range(5):
            yield i
    
    result = [i async for i in async_gen()]
  5. 新的标准库模块

    • secrets: 生成密码学安全的随机数
  6. 语法改进

    • 字典可以保持插入顺序
    • 变量注解允许在函数注释中使用 ->

Python 3.7 (2018年6月)

主要特性

  1. 数据类 (Data Classes)

    python 复制代码
    from dataclasses import dataclass
    
    @dataclass
    class Point:
        x: float
        y: float
  2. PEP 563 - 延迟评估的类型注解

    • 通过 from __future__ import annotations 实现
    • 类型注解在运行时不会被求值
  3. 上下文变量 (Context Variables)

    • 用于在异步代码中替代线程局部存储
  4. asyncio 改进

    • asyncio.run() 函数简化异步程序运行
    • asyncio.create_task() 创建任务
  5. 语法改进

    • 更简洁的异常链语法:raise new_exc from old_exc

Python 3.8 (2019年10月)

主要特性

  1. 赋值表达式 (海象运算符 :=)

    python 复制代码
    if (n := len(a)) > 10:
        print(f"List is too long ({n} elements, expected <= 10)")
  2. PEP 572 - 位置参数只标记

    python 复制代码
    def f(a, b, /, c, d, *, e, f):
        # a, b 只能按位置传递
        # e, f 只能按关键字传递
  3. f-strings 改进

    • 支持 f"{var=}" 语法,自动包含变量名和值
  4. 新的标准库模块

    • importlib.metadata: 访问包元数据
    • typing.TypedDict: 类型化字典
  5. 性能改进

    • 速度提升约 10-15%
    • pickle 序列化/反序列化速度提升

Python 3.9 (2020年10月)

主要特性

  1. 字典合并与更新运算符

    python 复制代码
    d1 = {'a': 1}
    d2 = {'b': 2}
    d3 = d1 | d2  # {'a': 1, 'b': 2}
    d1 |= d2      # d1 现在是 {'a': 1, 'b': 2}
  2. PEP 584 - 类型提示改进

    • list[str] 替代 List[str]
    • dict[str, int] 替代 Dict[str, int]
  3. 新的字符串方法

    • str.removeprefix(): 移除前缀
    • str.removesuffix(): 移除后缀
  4. 时区支持改进

    • zoneinfo 模块提供 IANA 时区数据库支持
  5. 其他改进

    • 放宽了装饰器语法限制
    • math.lcm()math.gcd() 函数

Python 3.10 (2021年10月)

主要特性

  1. 结构模式匹配 (Structural Pattern Matching)

    python 复制代码
    match status:
        case 200:
            print("OK")
        case 404:
            print("Not Found")
        case _:
            print("Other")
  2. 更精确的类型提示

    • Union[str, int] 可简化为 str | int
    • Optional[str] 可简化为 str | None
  3. PEP 647 - 用户定义的类型守卫

    python 复制代码
    def is_str_list(val: list[object]) -> TypeGuard[list[str]]:
        return all(isinstance(x, str) for x in val)
  4. 改进的错误信息

    • 更精确的语法错误定位
    • 更友好的错误提示

Python 3.11 (2022年10月)

主要特性

  1. 显著的性能提升

    • 速度提升约 10-60%
    • 平均比 Python 3.10 快 25%
  2. 异常改进

    • 异常回溯信息更详细
    • 显示异常发生的精确位置
  3. PEP 673 - Self 类型

    python 复制代码
    class Person:
        def set_name(self, name: str) -> Self:
            self.name = name
            return self
  4. 异步迭代器支持

    • async for 支持异步迭代器
  5. 新的标准库功能

    • tomllib: 解析 TOML 文件
    • dataclasses: 支持 kw_only 参数

Python 3.12 (2023年10月)

主要特性

  1. 更简洁的类型注解语法

    python 复制代码
    def greet(name: str, /, *, greeting: str = "Hello") -> str:
        return f"{greeting}, {name}!"
  2. PEP 695 - 类型变量注解

    python 复制代码
    type Point[T] = tuple[T, T]
    def scale[ScaleT](p: Point[ScaleT], factor: ScaleT) -> Point[ScaleT]:
        return (p[0] * factor, p[1] * factor)
  3. PEP 701 - f-strings 语法改进

    • 支持更复杂的表达式和嵌套
  4. 性能改进

    • 继续优化速度
    • 减少内存使用
  5. 标准库增强

    • pathlib 改进
    • json 模块性能提升

Python 3.13 (预计2024年10月)

主要特性(预发布版本)

  1. 进一步的性能优化

    • 持续改进解释器速度
    • 内存使用优化
  2. 语法改进

    • 可能会有更多类型系统增强
  3. 标准库更新

    • 模块重组和改进
    • 移除过时的功能
  4. 异步编程增强

    • 继续改进 asyncio 模块

Python 3.14 (规划中)

预期特性(基于社区讨论)

  1. 性能继续提升

    • 可能引入新的优化技术
  2. 类型系统增强

    • 更完善的类型检查支持
  3. 标准库现代化

    • 更新和改进现有模块
  4. 潜在的新功能

    • 基于社区需求的新特性

总结

Python 版本迭代中,主要的改进方向包括:

  • 性能提升:几乎每个版本都有显著的速度和内存优化
  • 类型系统增强:从 3.5 的类型提示开始,不断完善类型系统
  • 异步编程支持:从 3.5 的 async/await 开始,持续改进异步编程体验
  • 语法简化:引入更简洁的语法(如 f-strings、海象运算符等)
  • 标准库现代化:添加新模块,改进现有模块

这些改进使 Python 成为更强大、更高效、更易用的编程语言。

复制代码