Python OOP 语法补充学习路线
适合已有 Python 和 C++ OOP 基础,只想补 Python OOP 语法、接口、类型标注和工程常用写法。
| 顺序 | 教程 | 链接 | 内容一句话介绍 | 为什么要学 |
|---|---|---|---|---|
| 1 | Python 官方教程:Classes | https://docs.python.org/3/tutorial/classes.html | 讲 class、self、实例属性、继承、多继承、方法重写等基础语法。 |
先对齐 Python 和 C++ OOP 的语法差异。 |
| 2 | Real Python:Python Classes | https://realpython.com/python-classes/ | 用更完整例子讲类、对象、属性、方法、继承和 OOP 写法。 | 官方文档偏简略,这个适合快速补实际写法。 |
| 3 | Python 官方文档:dataclasses | https://docs.python.org/3/library/dataclasses.html | 讲 @dataclass 自动生成 __init__、__repr__ 等方法。 |
数据科学/AI 工程里常用来定义配置、样本、结果对象。 |
| 4 | Python 官方文档:typing | https://docs.python.org/3/library/typing.html | 讲函数参数、返回值、泛型、Optional、Callable、TypedDict 等类型标注。 |
解决“function 返回值规定”和数据接口约定。 |
| 5 | mypy:Type Hints Cheat Sheet | https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html | 类型标注速查表,覆盖函数、容器、类、可选值等常用写法。 | 比官方文档更适合当开发时的语法速查表。 |
| 6 | Python 官方文档:abc | https://docs.python.org/3/library/abc.html | 讲 ABC、@abstractmethod 抽象类和抽象方法。 |
对应 C++ 的抽象基类/纯虚函数,用来规定类接口。 |
| 7 | Python Typing:Protocols | https://typing.python.org/en/latest/spec/protocol.html | 讲 Protocol,用结构化类型定义“对象需要有哪些方法”。 |
Python 里比继承更灵活的接口写法,适合工程代码解耦。 |
| 8 | Real Python:OOP Learning Path | https://realpython.com/learning-paths/object-oriented-programming-oop-python/ | 系统覆盖 dataclass、constructor、super()、magic methods、property、继承、组合、SOLID。 |
作为最后查漏补缺,不需要从头全刷。 |
推荐学习顺序
Classes
→ Real Python Classes
→ dataclass
→ typing
→ mypy cheat sheet
→ abc
→ Protocol
→ Real Python OOP Path 查漏补缺