I want to know how to call subclass methods in the superclass.
5 Answers
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.

- 301
- 2
- 2
-
1Using C++ terminology it'd just be polymorphism. But with Python that's true for everything... – matsjoyce Nov 21 '14 at 17:54
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.
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.

- 16,136
- 7
- 59
- 83
-
1I 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? – Feb 03 '12 at 09:19
-
1@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 ;) ? – Tomasz Zieliński Feb 03 '12 at 09:23
-
4@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`. – glglgl Feb 03 '12 at 09:35
-
1Am I missing somtehing but both of this and glglgl answer are calling superclass methods from subclass which is not what OP asked for? – Bogdan Feb 03 '12 at 09:56
-
-
@zta1987: Great, then maybe you can accept one of the answers ;) ? – Tomasz Zieliński Feb 06 '12 at 06:48
-
@Bogdan Yes, you're missing something. ;-) Class A (superclass) is calling y() in class B (subclass), which is what OP asked about. – ziah75 Mar 26 '20 at 09:46
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)

- 89,107
- 13
- 149
- 217
-
-
1@Tichodroma Sorry, my fault. Should think while writing ;-) Corrected now. – glglgl Feb 03 '12 at 09:31
-
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 Oct 19 '12 at 18:03
-
@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. – glglgl Oct 19 '12 at 20:01
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

- 1
- 1

- 632
- 7
- 11