python pass different **kwargs to multiple functions python pass different **kwargs to multiple functions python python

python pass different **kwargs to multiple functions


There is no such mechanism. There is a proposal, PEP-448, whereby Python 3.5 and following generalize argument unpacking. Python 3.4 and previous don't support it. Best you can do in general:

def smoothy(x,y, kind='cubic', order = 3, kwargs_for_scatter={}, kwargs_for_plot={}):    yn_cor = interp1d(x, y, kind=kind, assume_sorted = False)    xn = np.linspace(np.min(x), np.max(x), len(x) * order)    plt.scatter(x,y, **kwargs_for_scatter)    plt.plot(xn, yn_cor(xn), **kwargs_for_plot);    return

Then pass in those options as dictionaries, not kwargs, to smoothy.

smoothy(x, y, 'cubic', 3, {...}, {...})

Because the variable names would then be possibly exposed to callers, you may want to rename them something shorter (perhaps scatter_options and plot_options).

Update: Python 3.5 and 3.6 are now mainstream, and they indeed support an extended unpacking syntax based on PEP-448.

>>> d = {'name': 'joe'}>>> e = {'age': 20}>>> { **d, **e }{'name': 'joe', 'age': 20}

This does not, however, help much in this kwargs-intended-for-multiple-destinations scenario. Even if the smoothy() function took a unified grab-bag of kwargs, you'd need to determine which of them were intended for which subfunctions. Messy at the very best. Multiple dict parameters, one intended to be passed to each kwarg-taking subfunction, still the best approach.


Another, more different approach

I realize I am a bit late to the party. However, I stumbled across a similar issue when dealing with a class composed of several other classes. I wanted to avoid passing dictionaries for each sub-class (or -function) and it would be very anti-dry to copy all the arguments of the component classes and additionally run the risk of having to update all of them at a later stage.

My solution is certainly not the shortest nor is it very nice, but I think it has a certain elegance to it. I modified the function smoothy below:

import inspectdef smoothy(x,y, kind='cubic', order = 3, **kwargs):    yn_cor = interp1d(x, y, kind=kind, assume_sorted = False)    xn = np.linspace(np.min(x), np.max(x), len(x) * order)    scatter_args = [k for k, v in inspect.signature(plt.scatter).parameters.items()]    scatter_dict = {k: kwargs.pop(k) for k in dict(kwargs) if k in scatter_args}    plt.scatter(x,y, **scatter_dict)    plot_args = [k for k, v in inspect.signature(plt.plot).parameters.items()]    plot_dict = {k: kwargs.pop(k) for k in dict(kwargs) if k in plot_args}    plt.plot(xn, yn_cor(xn), **plot_dict);    return

Explantion

To start off, make a list (scatter_args) of the arguments that the first function (scatter) accepts, using inspect.signature(). Then construct a new dictionary (scatter_dict) from kwargs, only extracting items that are also in our list of arguments. Using dict(kwargs) here ensures that we loop over a copy of kwargs, so that we can alter the original without running into errors. This new dictionary can then be passed to the function (scatter) and the steps are repeated for the next function.

A pitfall is that argument names in kwargs may not be repeated, since it is now a single dict. So for pre-built functions where you do not control argument names you might run into problems with this method.

This does allow me to then use said composed class as a parent (or sub) class (passing on the remainder of kwargs).


Use a Class for Help

I came to this question, because I needed to do something similar. After some thinking, it seemed that a class approach would help me. I hope this can extend to some others too.

import matplotlib.pyplot as pltimport numpy as npfrom scipy.interpolate import interp1dclass KWAs:    def __init__(self, algo):        self.algo = algo        self.kwargs_dict = {            'scatter_params':{},            'plot_params':{}        } # preloading group keys allows plotting when a kwarg group is absent.    def add_kwargs_to_dict(self, group_name, **kwargs):        self.kwargs_dict[group_name] = kwargs    def list_kwargs(self):        print('Listing all kwarg groups:')        for kwargs in self.kwargs_dict:            print('\tkwarg group {}: {}'.format(kwargs, self.kwargs_dict[kwargs]))        print()    def get_kwarg_group(self,group):        print('kwarg group {}: {}'.format(group, self.kwargs_dict[group]))        print()    def smoothy(self, x,y, kind='cubic', order = 3):        yn_cor = interp1d(x, y, kind=kind, assume_sorted = False)        xn = np.linspace(np.min(x), np.max(x), len(x) * order)        plt.scatter(x,y, **self.kwargs_dict['scatter_params'])        plt.plot(xn, yn_cor(xn), **self.kwargs_dict['plot_params'])        plt.show()kwas = KWAs('LSQ')N = 20colors = np.random.rand(N)area = (20 * np.random.rand(N))**2kwas.add_kwargs_to_dict('scatter_params', s=area, c=colors, alpha=0.5)kwas.add_kwargs_to_dict('plot_params', linewidth=2.0, color='r')kwas.list_kwargs()kwas.get_kwarg_group('scatter_params')kwas.get_kwarg_group('plot_params')x = []; y = []for i in range(N):    x.append(float(i)*np.pi/float(N))    y.append(np.sin(x[-1]))kwas.smoothy(x, y)

I didn't know what parameters you were trying to control with your kwargs, so I made some up from matplotlib examples. The above approach works, and you can add limitless numbers of kwarg groups to the class's kwargs dictionary and add additional methods that can all use the kwargs as desired.

Here's the output using the parameters that I added:enter image description here