Get a list/tuple/dict of the arguments passed to a function? Get a list/tuple/dict of the arguments passed to a function? python python

Get a list/tuple/dict of the arguments passed to a function?


You can use locals() to get a dict of the local variables in your function, like this:

def foo(a, b, c):    print locals()>>> foo(1, 2, 3){'a': 1, 'c': 3, 'b': 2}

This is a bit hackish, however, as locals() returns all variables in the local scope, not only the arguments passed to the function, so if you don't call it at the very top of the function the result might contain more information than you want:

def foo(a, b, c):    x = 4    y = 5    print locals()>>> foo(1, 2, 3){'y': 5, 'x': 4, 'c': 3, 'b': 2, 'a': 1}

I would rather construct a dict or list of the variables you need at the top of your function, as suggested in the other answers. It's more explicit and communicates the intent of your code in a more clear way, IMHO.


You can use the inspect module:

def foo(x):    return xinspect.getargspec(foo)Out[23]: ArgSpec(args=['x'], varargs=None, keywords=None, defaults=None)

This is a duplicate of this and this.


I would use *args or **kwargs and throw an exception if the arguments are not as expected

If you want to have the same errors than the ones checked by python you can do something like

def check_arguments(function_name,args,arg_names):    missing_count = len(arg_names) - len(args)    if missing_count > 0:        if missing_count == 1:            raise TypeError(function_name+"() missing 1 required positionnal argument: "+repr(arg_names[-1]))        else:            raise TypeError(function_name+"() missing "+str(missing_count)+" required positionnal argument: "+", ".join([repr(name) for name in arg_names][-missing_count:-1])+ " and "+repr(arg_names[-1]))

using with somethin like

def f(*args):    check_arguments("f",args,["a","b","c"])    #whatever you want    ...