🐍 PYTHON PY-01 ⏱ 8 min

现代 Python 极速通关:核心数据结构、推导式、生成器与类型注解

Modern Python Fast Track: Data Structures, Comprehensions, Generators & Type Hints

🌐 阅读模式 / View Mode:
📑 目录 / Table of Contents
🔑本课直击痛点 / No-Fluff Takeaways
  1. 选对容器性能差 1000 倍:list 查重是 O(n),set / dict 是 O(1)。
  2. 生成器(Generator) 让你用几 MB 内存优雅流式处理几十 GB 日志。
  3. Python 3.10+ 现代类型注解(list[str]、str | None、@dataclass)让代码既像脚本一样快写,又像强类型语言一样好维护。

Python Core Data Structures Complexity

1. 变量不是“盒子”,是贴在对象上的“标签” / Variables Are References, Not Boxes

中文

理解这一点能避开 80% 的 Python 诡异 Bug:变量赋值只是把标签贴到内存里的同一个对象上。

  • 不可变对象(int, str, tuple):修改会生成新对象,绝对安全。
  • 可变对象(list, dict, set):b = a 后改 b.append(1),a 也会一起变!需要独立副本请用 a.copy() 或 copy.deepcopy(a)。
  • 头号经典大坑——永远不要把 [] 或 {} 设为函数默认参数!默认参数只在函数定义时创建一次,所有调用会共享同一个列表。
EN

In Python, assignment binds a name to an object rather than copying it. Mutating a shared list or dict mutates every reference to it. Never use mutable defaults like def add(x, buf=[]):—use None instead:

1
2
3
4
5
6
7
8
9
10
# ❌ 经典大坑:多次调用会污染同一个列表
def bad_append(item: str, bucket: list[str] = []) -> list[str]:
bucket.append(item)
return bucket

# ✅ 正确写法:用 None 哨兵 + 现代联合类型 `| None`
def good_append(item: str, bucket: list[str] | None = None) -> list[str]:
result = [] if bucket is None else list(bucket)
result.append(item)
return result

2. 推导式与生成器:把 10 行循环写得既短又快 / Comprehensions & Generators

中文

Python 的列表/字典推导式不仅简洁,底层字节码还比手写 for + append 更快;当数据量巨大(如逐行读 10GB 文件)时,只需把方括号 [ ] 换成圆括号 ( ) 或用 yield,立刻变成惰性生成器(O(1) 内存):

EN

Comprehensions are both cleaner and faster than manual append loops. Swap [...] for (...) or yield to stream multi-gigabyte files in constant O(1) memory:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from pathlib import Path
from typing import Iterator

# 1. 字典推导式 + 集合极速过滤(O(1) 查找)
blocked = {"spam", "bot", "test"}
users = [{"id": 1, "name": "alice"}, {"id": 2, "name": "bot"}, {"id": 3, "name": "zan"}]
active_map: dict[int, str] = {u["id"]: u["name"] for u in users if u["name"] not in blocked}

# 2. 生成器:逐行流式过滤大日志,不把整份文件塞进内存
def iter_error_lines(log_path: Path) -> Iterator[str]:
with log_path.open("r", encoding="utf-8") as f:
for line in f:
if "ERROR" in line:
yield line.strip()

3. 现代类型注解 + @dataclass:告别“到处传裸字典” / Type Hints & Dataclasses

中文

用 dict 传来传去(order["usr_id"] 拼错字母要跑到线上才炸)是大型 Python 项目腐烂的元凶。现代 Python 直接用 @dataclass(slots=True, frozen=True):

  • 自动生成 __init__、__repr__、比较方法;
  • slots=True 省内存且防手误乱加属性;
  • frozen=True 不可变,防并发或跨函数意外篡改。
EN

Passing raw untyped dictionaries everywhere causes silent key typos. Use @dataclass(slots=True, frozen=True) for fast, immutable, IDE-autocompleted domain models:

1
2
3
4
5
6
7
8
9
10
11
from dataclasses import dataclass

@dataclass(slots=True, frozen=True)
class TaskItem:
id: int
title: str
tags: tuple[str, ...] = ()
priority: int = 1

task = TaskItem(id=42, title="Ship PostgreSQL guide", tags=("db", "pg"))
print(task) # TaskItem(id=42, title='Ship PostgreSQL guide', tags=('db', 'pg'), priority=1)

4. 延伸官方参考 / Official Docs