4

Note: It is not about Calling a parent method in a child class .super()

I have three classes, let's say Parent, Child1 and Child2. Child1 and 2 both have method Cry() and Parent class has another method e.g. MakeChildrenStopCry() in which Cry() is called. However, Parent class does not have method Cry(). Do I need to define Cry() in the Parent class?

Since I do not have any objects of the parent class and I always use the child classes, I simply created 'empty functions' since the inheritance will just overrule these empty functions with the functions from the Child classes.

def MakeChildrenStopCry(self):
   if self.Cry():
    self.DoWhateverToStopCry(self)
def Cry(self)
   return()

For full sample code you can check this but I think the above should be clear.

This is not causing any problems in my code, I just want to know what is done normally or if it is maybe better to setup my code differently.

Georgy
  • 12,464
  • 7
  • 65
  • 73
dejoma
  • 394
  • 1
  • 6
  • 18
  • 2
    You'd probably pass the an instance of the child to that method, how else would you know which child instance to call the `.cry()` method on otherwise? – Fynn Becker Jan 21 '19 at 10:06
  • Okay passing an instance of the child, let me look into that. It doesn't matter which child instance calls the cry method and you actually know this because you create the child instance and define what the function does in the specific child class. – dejoma Jan 21 '19 at 10:25
  • Well, that's kinda my point. You wouldn't really create specific child classes, you would create specific child instances from a single child class. Think of classes like blueprints that you create instances of with specific attributes. Likewise you wouldn't say all parents are 49 years old, you'd say parents have an `age` attribute and a specific parent (instance) could very well be 49 years old but they could also very well be almost any other age. Explaining the concepts of classes is really out of scope for SO though. – Fynn Becker Jan 21 '19 at 10:41
  • "you would create specific child instances from a single child class." So I have this method interpretSignal, but every childclass interprets the signal differently so this is not working. Thanks for help but you're going too much into detail ^^ It is merely about how Python handles these things, Serge Ballesta's answer completely answers this matter – dejoma Jan 21 '19 at 10:53

2 Answers2

6

Python is rather programmer confident at this level. You can always call a cry method from a class even if it is not defined in the class. Python will just trust you to provide an object that knows of the cry method at the time if will be called.

So this is perfectly fine:

class Parent:
    def makeChildrenStopCry(self):
        if self.cry():
            self.doWhateverToStopCry()

class Children(Parent):
    crying = False
    def makeCry(self):
        self.crying = True
    def doWhateverToStopCry(self):
        self.crying = False
    def cry(self):
        return self.crying

It gives in an interactive session:

>>> child = Children()
>>> child.makeCry()
>>> print(child.crying)
True
>>> child.makeChildrenStopCry()
>>> print(child.crying)
False
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
2

What if parent has abstract methods?

class Parent:
    def cry(self):
        raise NotImplementedError

    def doWhateverToStopCry(self):
        raise NotImplementedError

    def makeChildrenStopCry(self):
        if self.cry():
            self.doWhateverToStopCry()

class Children(Parent):
    crying = False
    def makeCry(self):
        self.crying = True
    def doWhateverToStopCry(self):
        self.crying = False
    def cry(self):
        return self.crying
Evren Bingøl
  • 1,306
  • 1
  • 20
  • 32