16

I want to know how to call subclass methods in the superclass.

1
  • Can you be more explicit with your question. Commented Feb 3, 2012 at 17:43

5 Answers 5

21

I believe this is a pattern used often.

class A(object):
    def x(self):
        self.y()

    def y(self):
        print('default behavior')


class B(A):
    def y(self):
        print('Child B behavior')

class C(A):
    def z(self):
        pass

>>>B().x()
Child B behavior
>>>C().x()
default behavior

It is sort of like an abstract class, but provides default behavior. Can't remember the name of the pattern off the top of my head though.

Sign up to request clarification or add additional context in comments.

1 Comment

Using C++ terminology it'd just be polymorphism. But with Python that's true for everything...
8

The point behind a subclass is that it extends and alters the behaviour of the superclass. The superclass can't know how a subclass will extend it.

Edit: But it is well possible that the superclass knows, that the subclass will extend it. Not sure, if this is good design, though.

Comments

5

Here's what I've just tried:

class A(object):
    def x(self): 
        print self.y()

class B(A):
    def y(self): 
        return 1

>>> B().x()
1

So unless you had some specific problem, just call a method from the subclass in the base class and it should just work.

7 Comments

I wonder why somebody wants to do this. Why do I want to write A in a way that i nows in advance how B will extend it?
@Tichodroma: Yeah, there's some code smell here. But maybe it's e.g. the lecturer of the OP who's out of sync with reality ;) ?
@Tichodroma maybe you don't know it? Let A be a base class which can be extended in, say, 3 or 4 ways. It doesn't know how these are implemented, only which signature they provide. I've seen such things often, e.g. in MySQLdb.
Am I missing somtehing but both of this and glglgl answer are calling superclass methods from subclass which is not what OP asked for?
Thank you very much...that's exactly what i wanted
|
3

This is a very broad question. As you don't provide example code, I'll just show the easiest example:

class A(object):
   def meth1(self, a):
       self.meth2(a, a*a)

class B(A):
   def meth2(self, a, b):
       return b / a

b = B()
b.meth1(10)

4 Comments

Where does a come from? Where the method meth?
@Tichodroma Sorry, my fault. Should think while writing ;-) Corrected now.
What if the classes are in separate modules? i.e. file2.py import file1 class TestCase(file1.TestBase): def execute(self): self.pass() file1.py class TestBase: def pass(self): print "PASS" testBase = TestBase() testBase.execute()
@ThePracticalOne Hard to read, but in your case, file2.py is not used at all. If you do testcase = TestCase() and testcase.execute(), it should work.
0

I think this is related to using abstract methods. The parent class defines methods that the subclass should implement and the parent class knows that these methods will be implemented. Similar structures exist for example in Java.

Using these in Python is discussed here well: Abstract methods in Python

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.