PyCharm警告我Call to __init__ of super class is missed
class AbstractBase(object):
def __init__(self):
raise NotImplementedError()
class RealChild(AbstractBase):
def __init__(self):
#super(RealChild, self).__init__() ####
print('do stuff')
child=RealChild()
但是如果我调用它,类AbstractBase
将引发{
我是只绵羊,不知道该怎么做:-)
您可以考虑using the ^{} Abstract Base Class module 将}的简单实现):
__init__
标记为抽象,然后继续从子类调用超类__init__
(并且,作为DorElias suggested,给超类__init__
一个{{{7}如果你想得到一个
因此,您已经获得了无法实现的抽象安全性,但同时,您仍然可以通过更改基类结构来更改所有子类结构,这是正确和恰当的。在
在抽象类中,将init函数中的异常替换为
此异常用于阻止您初始化抽象类的新实例(它是抽象的,所以您不能) 所以要么用“pass”,要么不听pycharm的话,不要叫super
您可以做一些难看的事情,并检查抽象类型的初始值设定项中的
self
类型,以确保它是子类型:我认为一个更“正常”的方法是不公开抽象基类型,并期望用户不要创建它。在
相关问题 更多 >
编程相关推荐