import functools


def patch(cls, attr):
    """Patch the function named attr in the object cls with the decorated function."""
    orig_func = getattr(cls, attr)
    @functools.wraps(orig_func)
    def decorator(func):
        def wrapped_func(*args, **kwargs):
            return func(orig_func, *args, **kwargs)
        setattr(cls, attr, wrapped_func)
        return orig_func
    return decorator