Python3 标准库概览

os, sys, collections, itertools, functools, hashlib, subprocess — with the idioms that matter.

Intermediate
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

TypeUseWhy not builtin
CounterCount occurrences, most_common()One line instead of a dict loop
defaultdictAuto-create missing valuesNo KeyError, no setdefault
dequeO(1) append/pop on both endslist.pop(0) is O(n)
namedtupleTuple with field namesReadable and still immutable
OrderedDictExplicit 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])     # 24

hashlib 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.

Friends & Partners