Skip to content

Classes calling super class init even though they don't inherit from it #22

@mbrukman

Description

@mbrukman

In optim_modules.py, we see the following code:

class OptimizationAlgorithm(abc.ABC):
def __init__(self, **kwargs):
nn.Module.__init__(self=self)

where class OptimizationAlgorithm is calling init on nn.Module even though it doesn't inherit from it (but probably should inherit?).

Then, lower in the same file, we see class Reinforce:

class Reinforce(OptimizationAlgorithm, nn.Module):
def __init__(
self, policy, gpu, max_grad_norm, lr, rw_norm, rw_clip, kl_ref_coeff, **kwargs
):
nn.Module.__init__(self=self)

which is also manually calling init on nn.Module and is explicitly inheriting from it.

I'm curious as to why you chose this pattern and what problem does it solve, or if this were incidental? It appears to be a non-standard pattern of initialization.

Would it make sense to have OptimizationAlgorithm inherit from nn.Module and call its init, and then have class Reinforce inherit from OptimizationAlgorithm and call its init, which will in turn, both inherit from and call init on nn.Module?

To simplify super class initialization with multiple inheritance, it's also possible to use super().__init__() without having to specify any of the superclasses, and it will take care of the ordering of parent class initialization.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions