I am fighting with one of my recurring nightmares of not knowing what is the best way to manage arguments for __init__
in a small hierarchy of classes. I have read several Q&A and articles on how good is to use super()
and I understand it, however many examples just handle the basic case of __init__
without arguments, that is seldom my case.
To make an example, I have a class root
with properties tags
and description
, it looks like:
class Root(object):
"""An object that can be tagged."""
def __init__(tags = None, description = None):
self.tags, self.description = tags, description
Now I can subclass, but what about the arguments tags
and description
? I can accept them explicitely in child class:
class Derived(Root):
def __init__(self,tags = None, description = None):
super().__init__(tags = tags, description = description)
or I can pass them by *args, **kwargs
The first solution forces me to rewrite the routine descriptor (and docs and docstrings) for all derived classes if I decide e.g. to add or remove an argument to/from Root
. The second works automatically, but it hides the argument names from the introspection; and needs to be manually filtered if any other routine needs to accept optional arguments from *args, **kwargs
.
A third way might be to explicity reassign the properties in child classes, without calling super.__init__
, but this looks duplicated code to me, so it's probably not good.
What is the best way to handle this situation?