Please take a look at the sample code below. The parent class, needs to call the setup()
function, but that function needs to be defined/overwritten in the child class. But the sample code below shows that when child
is initialized and called super()
, the super's method calls the parent setup()
which contains nothing.
Intended behavior is when child is instantiated, it calls parent's __init__
by using super()
and automatically call the setup function defined in the child. Setting Human.setup()
as abstract class also did not seem to work.
Google can only find information on how to call parent's method from child, but this case is the reverse, "How to call child's method from parent". Or is it not possible in python3?
The reason it needs to call the setup function of the child is due to each child class will be having a different setup procedure. But they share most of the methods defined in the parent class.
class Human(object):
def __init__(self):
self.setup()
def setup(self):
pass
class child(Human):
def __init__(self):
super()
def setup(self):
self.name = "test"
>>> A = child()
>>> A.name
AttributeError: 'child' object has no attribute 'name'