【问题标题】:Interface Inherited declaration接口继承声明
【发布时间】:2019-07-16 07:16:01
【问题描述】:

我正在尝试使用由 Github 开发人员开发的旧代码。该代码使用 zope.interface 库中的实现来声明类元素的接口。由于库中的实现在 Python 3.6 中不再起作用,我遇到了这个错误:

TypeError: Class advice impossible in Python3.  Use the @implementer class decorator instead.

一些网站已经解释了如何用 @implementer 替换实现以在 Python 3.6 上工作,例如 here。但是我还没有找到任何例子来解释当 zope.interface.implements 被用作继承时如何更新代码。代码如下所示:

from zope.interface import implements
class Car(implements(Moveable)):
     def __init__(self, x, v, lane, model: IDM, lane_change: LaneChange,
             length):
...

我想更新此代码以在 Python 3.6 上工作。这个我试过了

@implementer(Moveable) 
class Car:
     def __init__(self, x, v, lane, model: IDM, lane_change: LaneChange,
             length):

但它不起作用。 请帮我弄清楚如何使上述代码在 Python 3.6 中运行。

【问题讨论】:

    标签: python-3.x inheritance zope.interface


    【解决方案1】:

    以下步骤为我解决了这个问题,

    pip uninstall apex
    git clone https://www.github.com/nvidia/apex
    cd apex
    python3 setup.py install
    

    【讨论】:

      【解决方案2】:

      要使用implementer而不是Python建议的implements,您需要导入它而不是导入implements

      from zope.interface import implementer
      

      在您的代码中,它显示您仍在使用工具,根据提供的信息,这似乎是问题所在。希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-17
        • 1970-01-01
        • 2012-03-20
        • 2016-07-20
        • 1970-01-01
        • 2021-04-10
        相关资源
        最近更新 更多