您可以在協議中禁止子類化并使用typing.runtime_checkable: from typing import Protocol, runtime_checkable@runtime_checkableclass MyProtocol(Protocol): def foo(self): ... def __init_subclass__(cls, *args, **kwargs): raise TypeError 我不知道類型檢查器是否會忽略神奇方法__init_subclass__,因為我現在只能在手機上測試。 直接執行本議定書: >>> class MyClass:... def foo(self):... print('hello')...>>> isinstance(MyClass(), MyProtocol)True Inheritance agreement: >>> class MyClass(MyProtocol): pass...Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-