0

I have the following kind of superclass / subclass setup:

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"

I would like the SuperClass init to always call a not-yet-implemented do_something method. I'm using python 2.7. Perhaps ABC can do this, but it is there another way?

Thanks.

Eyal
  • 1,094
  • 10
  • 16
  • 2
    Right now, the init of SubClass is not calling the init of SuperClass, but that of the super class of the SuperClass, which is object. So init is not calling do_something at all. Are you sure this is the behavior you want for it? – Yanshuai Cao Oct 16 '11 at 17:29

1 Answers1

9

Your code is mostly correct, except for the use of super. You need to put the current class name in the super call, so it would be:

super(SubClass, self).__init__()

Since you put in the wrong class name, SuperClass.__init__ wasn't called, and as a result do_something wasn't called either.

interjay
  • 107,303
  • 21
  • 270
  • 254