nven nven - 4 months ago 9
Python Question

Multiple kwargs in a function call?

I have a simple function which is called like this:

arbitrary_function(**kwargs1, **kwargs2, **kwargs3)


It seems to compile fine on my local installation (python 3.5.1) but throws a SyntaxError when I compile it on a docker with python 3.4.5.

I'm not too sure why this behavior is present. Are multiple kwargs not allowed? Should I combine them before passing to function? It is more convenient to pass them individually, for example:

plot(**x_axis_params, **y_axis_params, **plot_params)


instead of

params = dict()

for specific_param in [x_axis_params, y_axis_params, plot_params]:
params.update(specific_param)

plot(**params)

Answer

That's a new feature introduced in Python 3.5. If you have to support Python 3.4, you're basically stuck with the update loop.

People have their own favored variations on how to combine multiple dicts into one, but the only one that's really a major improvement over the update loop is 3.5+ exclusive, so it doesn't help with this. (For reference, the new dict-merging syntax is {**kwargs1, **kwargs2, **kwargs3}.)