0

Given a child-parent structure like so:

class Parent:
    def __init__(self, param1=1, param2=2, param3=3, param4=4):
        """
        Parent docstring here
        :param param1: param1 stuff
        :param param2: param2 stuff
        :param param3: param3 stuff
        :param param4: param4 stuff
        """
        self.param1 = param1
        self.param2 = param2
        self.param3 = param3
        self.param4 = param4


class Child(Parent):
    def __init__(self, param1=1, param2=2, param3=3, param4=4,
                 child_param1='a', child_param2='b', child_param3='c'):
        """
        Child implementation of parent.
        :param param1: do I need this again???
        :param param2: do I need this again???
        :param param3: do I need this again???
        :param param4: do I need this again???
        :param child_param1: child parameter 1
        :param child_param2: child parameter 2
        :param child_param3: child parameter 3
        """
        super().__init__(param1, param2, param3, param4)
        self.child_param3 = child_param3
        self.child_param1 = child_param1
        self.child_param2 = child_param2

What is the correct way of implementing a child without repeating both the docstring of the parent and each individual parameter? I want the parameter description to be inherited from the parent. I also don't want to re-specify the default values every time I inherit from the parent. I could do this, but this doesn't seem like the right way:

class Child(Parent):
    def __init__(self, child_param1='a', child_param2='b', child_param3='c', **parent_args):
        super(**parent_args)
        # rest of the code goes here...
Raven
  • 648
  • 1
  • 7
  • 18
  • 1
    I guess you could do `self.__init__.__doc__ = super().__init__.doc__`. For the parameters your last example _does_ seem like the right way to me. – L3viathan Jul 26 '19 at 13:21
  • It is very common to pass down the kwargs to the parent. I've seen it in a lot of projects. I don't know if it's a good practice though. – Leogout Jul 26 '19 at 13:25

1 Answers1

1

You can't inherit default values from the parent, but you can have a 'special (unique)' default value and then use that to set the "real" default. E.g.

DEFAULT = object()


class Vehicle:
    DEFAULT_WHEELS = 4

    def __init__(self, wheels=DEFAULT):
        if wheels is DEFAULT:
            wheels = self.DEFAULT_WHEELS
        self.wheels = wheels


class Car(Vehicle):
    DEFAULT_COLOUR = "Red"

    def __init__(self, colour=DEFAULT, **kwargs):
        super().__init__(**kwargs)

        if colour is DEFAULT:
            colour = self.DEFAULT_COLOUR
        self.colour = colour


# You can also use the class-level default to modify the
# defaults without having to re-write the init:

class Tricycle(Vehicle):
    DEFAULT_WHEELS = 3
Tom Dalton
  • 6,122
  • 24
  • 35
  • Yeah, tried it out and this answer makes the most sense - have defaults as static variable in the parent and then override the defaults in the children. Thanks! – Raven Jul 29 '19 at 11:34