def sdmethod ( meth ) : sd = singledispatch ( meth ) def wrapper ( obj , * args , * * kwargs ) : return sd . dispatch ( args [ 0 ] . __class__ ) ( obj , * args , * * kwargs ) wrapper . register = sd . register wrapper . dispatch = sd . dispatch wrapper . registry = sd . registry wrapper . _clear_cache = sd . _clear_cache functools . update_wrapper ( wrapper , meth ) return wrapper
python can you monkey patch methods on an object
This is a hack to monkey patch sdproperty to work as expected with instance methods .
def sdmethod(meth):
    """
    This is a hack to monkey patch sdproperty to work as expected with instance methods.
    """
    sd = singledispatch(meth)

    def wrapper(obj, *args, **kwargs):
        return sd.dispatch(args[0].__class__)(obj, *args, **kwargs)

    wrapper.register = sd.register
    wrapper.dispatch = sd.dispatch
    wrapper.registry = sd.registry
    wrapper._clear_cache = sd._clear_cache
    functools.update_wrapper(wrapper, meth)
    return wrapper