如何从超类方法调用python子类方法?

如何从超类方法调用python子类方法?,python,subclass,init,super,abc,Python,Subclass,Init,Super,Abc,我有以下类型的超类/子类设置: class SuperClass(object): def __init__(self): self.do_something() # requires the do_something method always be called def do_something(self): raise NotImplementedError class SubClass(SuperClass): def __in

我有以下类型的超类/子类设置:

class SuperClass(object):
    def __init__(self):
        self.do_something() # requires the do_something method always be called

    def do_something(self):
        raise NotImplementedError

class SubClass(SuperClass):
    def __init__(self):
        super(SuperClass, self).__init__() # this should do_something 

    def do_something(self):
        print "hello"
我希望超类init总是调用一个尚未实现的do_something方法。我正在使用python 2.7。也许ABC可以做到这一点,但还有其他方法吗


谢谢。

除了使用
super
之外,您的代码基本上是正确的。您需要将当前类名放入
super
调用中,因此它将是:

super(SubClass, self).__init__()

由于您输入了错误的类名,
超类。\uuuu init\uuuu
没有被调用,因此
do\u something
也没有被调用。

现在,子类的init不是调用超类的init,而是调用超类的init,即object。所以init根本不调用dou。你确定这是你想要的行为吗?