pythonpython-3.xparameter-passingvariadic-functions

How can we print the argument-list from args and kwargs?


Suppose that we have a variadic function, such as the following:

def foo(*args, **kwargs):
    pass

I would like to edit foo, so that it prints the argument-list. For example, we want the following code...

foo(98, 99, 100, a = 1, b = 2, c = 3)

... to print...

98, 99, 100, a = 1, b = 2, c = 3   

Solution

  • This will do pretty much what you want:

    def foo(*args, **kwargs):
        print(args + tuple('{} = {}'.format(key,val) for key,val in kwargs.items()))