Tuesday, 20 August 2013

How do I call a method from a superclass, whos definition is completely deferred to the subclasses?

How do I call a method from a superclass, whos definition is completely
deferred to the subclasses?

I want to envoke a method in my code in a supercass, to do some subclass-
specific processing before continuing on. I come to python recently from
C#... there, I'd probably use an interface. Here's the gist of it (as I
picture it, but it's not working):
class superClass:
def do_specific_stuff(self): #To be implemented entirely by the subclass,
#but called from the superclass
pass
def do_general_stuff1(self):
#do misc
def do_general_stuff2(self):
#do more misc
def main_general_stuff(self):
do_general_stuff1()
do_specific_stuff()
do_general_stuff2()
I have a rather complicated implementation of this; this example is
exactly what I need and far less painful to understand for a first- time
viewer. Calling do_specific_stuff() at the moment gives me the error
'global name 'do_specific_stuff' is not defined.
When I add 'self' as in self.do_specific_stuff I get the error
'TypeError: do_specific_stuff() takes 0 positional arguments but 1 was
given.' Any takers? Thanks in advance...

No comments:

Post a Comment