def add_delegate(*delegates):

    def init_wrapper(cls, delegate_class_list):
        """Returns a function that calls the wrapped class' __init__ function
        and then creates an instance of each delegate class in delegate_class_list.
        """
        fnc = cls.__init__
        spec = getargspec(fnc)
        sig = formatargspec(*spec)
        template = ["if not hasattr(self, '_delegates_'): self._delegates_ = {}"]
        for name, delegate in delegate_class_list:
            template.append('self.%s = %s(self)' % (name, delegate.__name__))
            template.append("self._delegates_['%s'] = self.%s" % (name,name))
        template.append('old_init__(%s)' % ','.join(spec[0]))
        f = FunctionMaker.create('__init__%s' % sig, '\n'.join(template), 
                                 dict([(c.__name__,c) for n,c in delegate_class_list]+
                                      [('old_init__',fnc)]),
                                 doc=fnc.__doc__, defaults=spec[3])
        return types.MethodType(f, None, cls)
        
    def _add_delegate(cls):
        """Returns the given class, updated with a new __init__ function that wraps the
        old one and adds instantiation of the delegate, and adds new member functions
        that match the public members in the delegate class.  Any public members in the
        delegate that have a name matching anything in the scoping object are ignored.
        """
        member_set = set()

        for klass in getmro(cls):
            member_set.update(klass.__dict__.keys())
    
        listofdels = []
        for tup in delegates:
            if isinstance(tup, tuple):
                delegatename, delegate = tup
            else: # assume no name given
                delegate = tup
                delegatename = None
            if not isinstance(delegate, type):
                raise TypeError("Can't add delegate to class %s because delegate is not a class"%
                                cls.__name__)
            if not delegatename:
                delegatename = '_%s' % delegate.__name__.lower()
                
            listofdels.append((delegatename, delegate))
            
            alldict = {}

            for interface in implementedBy(delegate):
                classImplements(cls, interface)

            for klass in getmro(delegate):
                if hasattr(klass, '_do_not_promote'):
                    skip = klass._do_not_promote
                else:
                    skip = []
                for k,v in getmembers(klass, ismethod):
                    if not k.startswith('_') and k not in alldict and k not in member_set and k not in skip:
                        member_set.add(k)
                        setattr(cls, k, forwarder(cls, v, delegatename))
            cls.__init__ = init_wrapper(cls, listofdels)
        return cls
    return _add_delegate
A class decorator that takes delegate classes or (name,delegate) tuples as
dataset/ETHPy150Open OpenMDAO/OpenMDAO-Framework/openmdao.util/src/openmdao/util/decorators.py/add_delegate