python历个版本的主要变动有哪些
- User
python历个版本的主要变动有哪些
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
python历个版本的主要变动有哪些
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Python 自 1991 年诞生以来,经历了多次重大更新,每个版本都引入了新特性、优化或语法改进。以下是各主要版本的核心变动概览:
**kwargs)、复数支持、更强的模块系统。type 和 class 合并),属性访问控制(property)。enumerate() 函数、set 数据类型。@decorator)、subprocess 模块替代 os.system。with 语句(上下文管理器)、try/except/finally 合并。print() 函数)。print 变为函数(print())。str 是 Unicode,bytes 是字节序列)。/),// 为整除。object)。dict.keys() 返回视图而非列表)。format() 方法增强。argparse 模块标准化。yield from 语法(协程支持)、venv 模块(虚拟环境)。asyncio 库(异步编程)、pathlib 模块(路径操作)。def foo(x: int) -> str:)。@ 矩阵乘法运算符:用于数值计算库(如 NumPy)。async/await 语法:替代 yield from,简化异步代码。f"Hello {name}"。dict 成为有序容器。x: int = 1)。dataclasses):自动生成特殊方法(如 __init__)。breakpoint() 函数:调试工具。asyncio 性能优化。:=):赋值表达式(if (n := len(x)) > 0:)。/):强制参数为位置参数(def f(a, /, b):)。functools.cached_property:缓存属性装饰器。|):dict1 | dict2。list[int] 替代 List[int](需 from __future__ import annotations)。str.removeprefix() 和 str.removesuffix()。match/case):类似 switch 的语法。
match x:
case 1: print("One")
case _: print("Other")
ExceptionGroup):同时处理多个异常。Self 类型注解(表示返回自身类型)。@override 装饰器(显式标记方法覆盖)。f-strings 支持任意表达式:如 f"{x=}" 输出 x=value。mypy)。asyncio 和 async/await 成为标准。2to3 工具或手动适配 3.x。如需更详细的特性列表,可参考 Python 官方文档。