Skip to main content

Python decorator function to dump arguments passed to a function before calling it.

def dump_args(func):
    """
    Easy Dump of Function Arguments.
    Decorator function to dump arguments passed to a function before calling it.

    Example Usage:

        @dump_args
        def f1(a, b, c):
            print a + b + c

        f1(1, 2, 3)

    https://wiki.python.org/moin/PythonDecoratorLibrary#Easy_Dump_of_Function_Arguments
    """
    argnames = func.func_code.co_varnames[:func.func_code.co_argcount]
    fname = func.func_name

    def echo_func(*args, **kwargs):
        print fname, ":", ', '.join(
            '%s=%r' % entry
            for entry in zip(argnames, args) + kwargs.items())
        return func(*args, **kwargs)

    return echo_func