Calling overridden parent class method in parent class

Viewed 506

I'm trying to override two parent class functions train and eval in a ChildClass. In the parent class, eval() basically calls train(). However, I realize that when I write my code as below, eval() in the parent class is trying to call the function train() in ChildClass - I would like eval() in the parent class to call train() in the parent class instead.

I'm just wondering if there is any clean solutions to make changes to ChildClass that would allow the parent class to call the parent train() function?

class ChildClass(nn.Module):
    def __init__(self):
        super(ChildClass, self).__init__()

    def train(self):
        super(ChildClass, self).train()

    def eval(self):
        super(ChildClass, self).eval()

Parent class is in a Python Package (pytorch), so no changes should be made:

class Module(object):
    #...

    def train(self, mode=True):
        # ...
        return self

    def eval(self):
        return self.train(False)
1 Answers

Your overridden methods are not doing anything except invoking the parent (at least from the code you have shared).

So, I think you want to have a method that has the same steps as in train()/eval(). I guess you don't need to override train() or eval(), instead add method(s) in your child class and call parent train()/eval() in whichever order you want to mix them.

Related