使用接口包会导致issubclass()错误

时间:2018-10-24 14:41:09

标签: python interface

我目前正在一个项目中,我们决定使用interface库而不是abc库。但是,当我运行程序时,会弹出以下错误:

Traceback (most recent call last):
  File "runner.py", line 1, in <module>
    from exchanges import *
  File "/Users/**/Projects/jimmy/characterlib/__init__.py", line 2, in <module>
    from exchanges import hitbtc
  File "/Users/**/Projects/jimmy/characterlib/soldier.py", line 5, in <module>
    class Soldier(implements(ICharacter)):
  File "/usr/local/lib/python3.7/site-packages/interface/interface.py", line 490, in implements
    if not issubclass(I, Interface):
TypeError: issubclass() arg 1 must be a class

现在我的设置非常简单:

soldier.py:

from interface import implements, Interface
import characterlib.ICharacter as ICharacter

class Soldier(implements(ICharacter)):

ICharacter:

from interface import Interface

class ICharacter(Interface):
    def walk(self): pass

初始化 .py:

import characterlib.ICharacter
import characterlib.Soldier
如您所见,

非常简单的设置,但是,我无法使其正常运行。这怎么可能?

有人在这个主题上有更多的经验吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

您的import有点错误。

import characterlib.ICharacter as ICharacter

表示ICharacter是一个模块,而不是接口。只需更改类声明即可解决:

class Soldier(implements(ICharacter.ICharacter)):
    def walk(self):
        pass