5

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)
Mahir Islam
  • 1,941
  • 2
  • 12
  • 33
matohak
  • 535
  • 4
  • 19
  • If you want the parent class to call the parent train implementation, why are you overriding it in the child class? What you are experiencing is the intended behavior. – mehdix Dec 18 '18 at 13:06
  • 1) I bet 100$ that yours is an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). 2) Not really. In python all methods are "virtual". That is how virtual methods work. Now, in CPython you could do something nasty like checking the stackframes using [`inspect`](https://docs.python.org/3.7/library/inspect.html#the-interpreter-stack) and do something different when you are called from the parent class, but this is a huge hack. – Giacomo Alzetta Dec 18 '18 at 13:08
  • I have a ChildClass object that I want to call eval() and train() with that's why I want to override these functions. – matohak Dec 18 '18 at 13:09
  • 2
    @matohak No, that's not true. You do not want to just "call those methods." That's not a requirement **ever**. You want to write a program that achieve a task in machine learning using PyTorch. You assume you have to subclass that class in that way because that's how you think . *you are probably wrong* and there is a different solution that doesn't need this hack. But in order to find the correct way of achieving your final goal you will have to explain exactly what you want to do with PyTorch. – Giacomo Alzetta Dec 18 '18 at 13:15
  • Inheritance may not be what you want. `Module.eval` isn't calling any particular class's `train` method; it's leaving that choice up to whatever object `self` is bound to. – chepner Dec 18 '18 at 22:06

1 Answers1

0

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.

Mohana Rao
  • 881
  • 5
  • 19