def fncountcall(function, args):
    """
    Count the number of calls of a function.  Call once at the start of a
    routine, and henceforth use the returned function instead of the original.
    The other return value will contain the number of function calls updated.
    (Trick from scipy.optimize.)
    """
    ncalls = [0]

    def fn_wrapper(x):
        ncalls[0] += 1
        return function(x, *args)

    return fn_wrapper, ncalls