Python 是動態類型語言, 只在運行時做 Duck Typing 檢查.
- 利: 靈活, 方便
- 弊: 代碼混亂, 缺少規范
標準自帶兩類接口支持: abc 和 typing.Protocol, 有他們協助給天馬行空的程序員套上枷鎖, Python 的大工程才可以"上道"

abc
abc 就是 Abstract Base Class, 虛基類. 跟 Java, C++ 中的虛基類是一個意思, 可以對派生類提供實例化時的動態檢查, 確保虛擬接口 (abstractmethod) 都有實現
import abcclass Base(abc.ABC): @abstractmethod def foo(self, s: str): """abc interface demo """class Invalid(Base): pass class Child(Base): def foo(self): pass c = Child()assert isinstance(c, Base)# TypeError: Can't instantiate abstract class Invalid with abstract methods fooi = Invalid()
也提供了非侵入式的虛基類關聯方法
from abc import ABCclass MyABC(ABC): passMyABC.register(tuple)assert issubclass(tuple, MyABC)assert isinstance((), MyABC)
- 檢查時機: 在運行中當派生類實例化時
- 檢查范圍: 只確保 abstractmethod 是否在派生類中有相同函數名實現, 并不檢查實現的參數和返回值是否相同. 只看名字不比簽名
- 代碼影響: 侵入式, 需要繼承. 也有手工非侵入式方案
typing.Protocol
structure subtyping (static duck-typing)
import typingclass Countable(typing.Protocol): def count(self, who: str) -> int: """support count """ class Counter: def count(self, who: str) -> int: return 0 c = Counter()def f(c: Countable): c.count("bill")
- 檢查時機: 靜態類型檢查接口使用方, 例如 mypy
- 檢查范圍: 確保實現類按照簽名實現了接口的全部函數
- 代碼影響: 非侵入式, 不需要繼承
比較
abc 類似 c++ 中的虛基類, typing.Protocol 則好比 c++ 中的 concept.
當然, Python 是動態語言, 在 typing.runtime_checkable 和 abc.abstractmethod 加成后, typing.Protocol 動靜兩相宜
import typing@typing.runtime_checkableclass Countable(typing.Protocol): @abc.abstractmethod def count(self, who: str) -> int: """support count """ class Counter: def count(self, who: str) -> int: return 0 assert issubclass(Counter, Countable)c = Counter()assert isinstance(c, Countable)def f(c: Countable): assert isinstance(c, Countable) print(c.count("bill")) f(c)class InvalidCounter(Countable): def c(self): pass # TypeError: Can't instantiate abstract class InvalidCounter with abstract methods counti = InvalidCounter()
上面這個終極解決方案兼有兩者的優點:
- 靜態類型檢查時會確保是否在派生類中有相同簽名的實現
- 動態運行時, 會檢查是否同名函數存在
- 代碼影響: 自動非侵入式, 不需要繼承, 也無需手工注冊