def delegate(attribute_name, method_names):
    """Pass the call to the attribute called attribute_name for every method listed in method_names."""
    # hack for python 2.7 as nonlocal is not available
    info = {
        'attribute': attribute_name,
        'methods': method_names
    }

    def decorator(cls):
        """Decorate class."""
        attribute = info['attribute']
        if attribute.startswith("__"):
            attribute = "_" + cls.__name__ + attribute
        for name in info['methods']:
            setattr(cls, name, eval("lambda self, *a, **kw: "
                                    "self.{0}.{1}(*a, **kw)".format(attribute, name)))
        return cls
    return decorator
def delegate ( attribute_name , method_names ) : info = { 'attribute' : attribute_name , 'methods' : method_names } def decorator ( cls ) : attribute = info [ 'attribute' ] if attribute . startswith ( "__" ) : attribute = "_" + cls . __name__ + attribute for name in info [ 'methods' ] : setattr ( cls , name , eval ( "lambda self, *a, **kw: " "self.{0}.{1}(*a, **kw)" . format ( attribute , name ) ) ) return cls return decorator
Pass the call to the attribute called attribute_name for every method listed in method_names.