Python 今天关于用于文档目的的接口如何?

How's Python today concerning Interfaces for documentation purposes?

我很久以前就读过 this post,我尝试创建自己的简单示例:

class IExample(Interface):
    """ An object that serves as an Example."""

class IExamp(IExample):
    """ Examp object representing the current state of Example."""

    def set_state(state):
        """ Sets the state of Example."""

    def get_state():
        """ Gets the state of Example."""

class Example():

    def __init__(self, state=0):
        self.state = state

class Examp(Example):

    def __init__(self, state=0):
        Example.__init__(self, state)

    def set_state(self, state):
        self.state = state

    def get_state(self):
        return self.state

e = Examp()
print(e.get_state())

我得到一个错误(这对许多老派 Python 程序员来说应该是显而易见的,但对我来说不是,因为我在 2012 年或其他什么时候学会了 Python)。

Traceback (most recent call last):
  File "python", line 1, in <module>
NameError: name 'Interface' is not defined

当我在 Google 上键入 "Python 3.4 Interface" 时,我得到了十万个与图形用户界面编程相关的链接,而不是界面(我还搜索了 Python 文档, 而且什么也没有)。

那么,Python 今天关于用于文档目的的接口的情况如何?他们是否将这个概念埋得太深,以至于再也没有人能找到任何关于它的东西了?它是旧 python 版本领域中丢失的东西吗?如果是这样,为什么一直 "dropped off"?某处有关于它的 "official" post 吗?在 Python 今天 人们真的将接口用于文档目的吗?如果他们这样做了,他们是怎么做到的?

Interfaceclass来自zope.interface模块,这就解释了为什么你没有在官方文档中找到它。

事实是,Zope 不再 经常使用(至少,很多新项目不再使用)。您仍然会在遗留代码中看到它,但不会看到很多新代码。

如果你想坚持使用 Python stdlib 源代码,你可以使用 abstract base classes 来达到类似的目的。

不过,老实说,接口在 Python 代码中已经用得不多了。