← 返回 Day 42 正文

Day 42 · 练习与清单

Day 42 完成清单

今日学习:类型提示与检查

练习题

练习 1:为函数添加类型注解

python
# 给以下函数添加完整的类型注解,不改变实现
def add(a, b):
    return a + b

def get_user_name(users, uid):
    return users.get(uid)

def process_items(items, callback):
    result = []
    for item in items:
        result.append(callback(item))
    return result

def batch_process(*args, **kwargs):
    results = {}
    for key, value in kwargs.items():
        results[key] = sum(value)
    return results

练习 2:实现泛型队列

用 Generic[T] 实现一个泛型队列 Queue[T],包含: - enqueue(item: T) -> None:入队 - dequeue() -> T:出队(空队列抛出 IndexError) - peek() -> T:查看队首 - is_empty() -> bool:是否为空 - __len__() -> int

练习 3:Protocol 实现可排序比较

定义一个 Comparable Protocol,要求实现 __lt__ 方法。 然后写一个泛型排序函数 sort_desc(items: List[T]) -> List[T], 要求 T 满足 Comparable 协议。

练习 4:pydantic 数据校验

假设有一段 JSON 数据需要校验:

json
{
  "title": "Python 类型提示",
  "author": "Alice",
  "year": 2024,
  "tags": ["python", "typing"],
  "pages": null
}

用 pydantic 定义 Book 模型,要求: - title: str, 必填 - author: str, 必填 - year: int, 必须 >= 1900 - tags: List[str], 默认空列表 - pages: Optional[int], 可选

练习 5:mypy 严格模式检查

在项目根目录创建 pyproject.toml,配置 mypy 严格模式:

toml
[tool.mypy]
strict = true

然后对 01-typing-basics.py 运行 mypy 检查,修复所有报告的类型错误。


附加资源