human
Complete this python program:
def grad(fun, argnum=0):
assistant
#Here is the Python code you requested.

    """Returns a function which computes the gradient of `fun` with respect to
    positional argument number `argnum`. The returned function takes the same
    arguments as `fun`, but returns the gradient instead. The function `fun`
    should be scalar-valued. The gradient has the same type as the argument.
    """
    @attach_name_and_doc(fun, argnum, 'Gradient')
    @add_error_hints
    def gradfun(*args, **kwargs):
        args = list(args)
        args[argnum] = safe_type(args[argnum])
        (vjp, ans) = make_vjp(fun, argnum)(*args, **kwargs)
        return vjp(vspace(ans).ones())
    return gradfun