Class hierarchies and constructors are interconnected. Parameters from the child class must be passed to their parents.
So, in Python we get something like this:
class Parent(object): def __init__(self, a, b, c, ka=None, kb=None, kc=None):
Imagine this with a dozen child classes and lots of options. Adding new parameters becomes very tedious.
Of course, you can completely abandon named parameters and use * args and ** kwargs, but this makes the method declarations ambiguous.
Is there a template for elegantly solving this issue in Python (2.6)?
By "elegant" is meant that I would like to reduce the number of times that parameters appear. a, b, c, ka, kb, kc appear 3 times: in the Child constructor, in the super () call for the parent, and in the parent constructor.
Ideally, I would like to specify the parameters for the parent init once, and in Child init, specify only additional parameters.
I would like to do something like this:
class Parent(object): def __init__(self, a, b, c, ka=None, kb=None, kc=None): print 'Parent: ', a, b, c, ka, kb, kc class Child(Parent): def __init__(self, d, e, f, kd='d', ke='e', kf='f', *args, **kwargs): super(Child, self).__init__(*args, **kwargs) print 'Child: ', d, e, f, kd, ke, kf x = Child(1, 2, 3, 4, 5, 6, ka='a', kb='b', kc='c', kd='d', ke='e', kf='f')
This, unfortunately, does not work, since 4, 5, 6 are eventually assigned kd, ke, kf.
Is there some kind of elegant python template to accomplish the above?