Python3 标准库概览
os, sys, collections, itertools, functools, hashlib, subprocess — with the idioms that matter.
Key PointsKnow Counter/deque/defaultdict, itertools, functools.partial/lru_cache and subprocess.run — they replace a lot of hand-written loops.
os and sys
Python
import os, sys
os.getenv("DATABASE_URL", "sqlite:///local.db") # 读环境变量,带默认值
os.environ["MODE"] = "prod" # 设置(仅当前进程)
os.cpu_count(), os.getpid()
sys.argv # 命令行参数,argv[0] 是脚本名
sys.exit(1) # 以非零码退出
sys.version_info # (3, 12, 3, 'final', 0)
sys.stderr.write("oops\n")collections
| Type | Use | Why not builtin |
|---|---|---|
| Counter | Count occurrences, most_common() | One line instead of a dict loop |
| defaultdict | Auto-create missing values | No KeyError, no setdefault |
| deque | O(1) append/pop on both ends | list.pop(0) is O(n) |
| namedtuple | Tuple with field names | Readable and still immutable |
| OrderedDict | Explicit ordering (dict is ordered since 3.7) | Mostly legacy now |
Python
from collections import Counter, defaultdict, deque
Counter("hello").most_common(2) # [('l', 2), ('h', 1)]
groups = defaultdict(list)
groups["a"].append(1) # 自动建列表
q = deque([1, 2, 3], maxlen=3)
q.append(4) # 左侧自动挤出 -> deque([2, 3, 4])itertools
Python
import itertools as it
list(it.islice(it.count(10), 3)) # [10, 11, 12] 无限计数器切片
list(it.chain([1, 2], [3, 4])) # [1, 2, 3, 4]
list(it.product("ab", repeat=2)) # [('a','a'), ('a','b'), ('b','a'), ('b','b')]
list(it.combinations([1, 2, 3], 2)) # [(1,2), (1,3), (2,3)]
list(it.groupby("aabbb")) # 需先排序,产出 (key, group)functools
Python
from functools import lru_cache, partial, reduce, wraps
@lru_cache(maxsize=128) # 记忆化:递归/重复调用提速
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
connect = partial(open, encoding="utf-8") # 预置参数
reduce(lambda a, b: a * b, [1, 2, 3, 4]) # 24hashlib and subprocess
Python
import hashlib, subprocess
hashlib.sha256(b"payload").hexdigest()
hashlib.md5(b"x", usedforsecurity=False).hexdigest() # 校验用,不要用于安全
# 跑外部命令,不要用 shell=True 拼接用户输入
r = subprocess.run(["ls", "-l"], capture_output=True, text=True, timeout=10)
print(r.returncode, r.stdout)@lru_cache keeps arguments alive — never cache a function that takes large objects, and clear the cache when the underlying data changes.os 与 sys
Python
import os, sys
os.getenv("DATABASE_URL", "sqlite:///local.db") # 读环境变量,带默认值
os.environ["MODE"] = "prod" # 设置(仅当前进程)
os.cpu_count(), os.getpid()
sys.argv # 命令行参数,argv[0] 是脚本名
sys.exit(1) # 以非零码退出
sys.version_info # (3, 12, 3, 'final', 0)
sys.stderr.write("oops\n")collections
| 类型 | 用途 | 相比内置的优势 |
|---|---|---|
| Counter | 计数、取最高频 | 一行替代字典循环 |
| defaultdict | 缺失键自动创建 | 不会 KeyError,不用 setdefault |
| deque | 两端 O(1) 增删 | list.pop(0) 是 O(n) |
| namedtuple | 带字段名的元组 | 可读且不可变 |
| OrderedDict | 显式有序(3.7 起 dict 已有序) | 如今多为历史用法 |
Python
from collections import Counter, defaultdict, deque
Counter("hello").most_common(2) # [('l', 2), ('h', 1)]
groups = defaultdict(list)
groups["a"].append(1) # 自动建列表
q = deque([1, 2, 3], maxlen=3)
q.append(4) # 左侧自动挤出 -> deque([2, 3, 4])itertools
Python
import itertools as it
list(it.islice(it.count(10), 3)) # [10, 11, 12] 无限计数器切片
list(it.chain([1, 2], [3, 4])) # [1, 2, 3, 4]
list(it.product("ab", repeat=2)) # [('a','a'), ('a','b'), ('b','a'), ('b','b')]
list(it.combinations([1, 2, 3], 2)) # [(1,2), (1,3), (2,3)]
list(it.groupby("aabbb")) # 需先排序,产出 (key, group)functools
Python
from functools import lru_cache, partial, reduce, wraps
@lru_cache(maxsize=128) # 记忆化:递归/重复调用提速
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
connect = partial(open, encoding="utf-8") # 预置参数
reduce(lambda a, b: a * b, [1, 2, 3, 4]) # 24hashlib 与 subprocess
Python
import hashlib, subprocess
hashlib.sha256(b"payload").hexdigest()
hashlib.md5(b"x", usedforsecurity=False).hexdigest() # 校验用,不要用于安全
# 跑外部命令,不要用 shell=True 拼接用户输入
r = subprocess.run(["ls", "-l"], capture_output=True, text=True, timeout=10)
print(r.returncode, r.stdout)@lru_cache 会一直持有参数 —— 不要给接收大对象的函数加缓存,底层数据变化时要清缓存。