我目前正在为python 2开发,我正在尝试使用抽象基类来模拟接口。我有一个接口,该接口的基本实现和许多扩展基本实现的子类。它看起来像这样:
class Interface(object):
__metaclass__ = ABCMeta
class IAudit(Interface):
@abstractproperty
def timestamp(self):
raise NotImplementedError()
@abstractproperty
def audit_type(self):
raise NotImplementedError()
class BaseAudit(IAudit):
def __init__(self, audit_type):
# init logic
pass
@property
def timestamp(self):
return self._timestamp
@property
def audit_type(self):
return self._audit_type
class ConcreteAudit(BaseAudit):
def __init__(self, audit_type):
# init logic
super(ConcreteAudit, self).__init__(audit_type)
pass
然而,PyCharm通知我ConcreteAudit
应该实现所有抽象方法。但是,BaseAudit
(未指定为abc)已实现这些方法,ConcreteAudit
是BaseAudit
的子类。为什么PyCharm警告我?它不应该通过IAudit
检测到BaseAudit
的合同已经实现了吗?
答案 0 :(得分:2)
为什么PyCharm警告你?
因为所有 Python IDE都很糟糕,这就是原因。
每当一个实习生/初级程序员/同伴告诉我我写的东西对他不起作用时,我告诉他我没有讨论它,直到他通过从命令行执行Python脚本或从股票翻译。 99%的情况下,问题就消失了。
他们为什么吮吸?甘拜下风。但它们有时会隐藏异常,有时会让你有可能输入你不知道的东西,并且有时候所有人都决定(在这种情况下)某些东西是有问题的,因为在股票解释器上运行的真实程序根本就不会有。的问题。
我尝试使用Python 2.7和Python 3.4中的代码,只要我在顶部添加from abc import ABCMeta, abstractproperty
,它就会运行很好的。
所以只需放弃PyCharm,或更新标签以显示错误所在的位置。