One solution is to allow your class to accept extra keyword parameters
class OneAttribute:
def __init__(self,a=1,**kwargs):
self.a = a
class TwoAttributes:
def __init__(self,b=2,c=3,**kwargs):
self.b = b
self.c = c
Then you can simply accept keyword parameters in the container class
class SuperClass:
def __init__(self, **kwargs):
self.one = OneAttribute(**kwargs)
self.two = TwoAttributes(**kwargs)
The downside is that if you pass an extra parameter to the two inner classes in other it would go undetected.
Avoiding this problem is possible, but requires some black magic: first you just declare the inner classes as usual:
class OneAttribute:
def __init__(self,a=1):
self.a = a
class TwoAttributes:
def __init__(self,b=2,c=3):
self.b = b
self.c = c
then you discover using introspection what are the parameters they expect
def parms_of(f):
return f.__code__.co_varnames[:f.__code__.co_argcount]
and pass only those to the inner classes constructor
def filter_kwargs(kwargs, f):
s = set(parms_of(f))
return dict((k, v) for k, v in kwargs.items()
if k in s)
class SuperClass:
def __init__(self, **kwargs):
self.one = OneAttribute(**filter_kwargs(kwargs, OneAttribute.__init__))
self.two = TwoAttributes(**filter_kwargs(kwargs, TwoAttributes.__init__))