python *args and **kwargs
<p>I have seen the previous stack overflow posts on this topic, but I am still unable to create use these two commands when I try to run my function. I have coded a demo example of a simple moving average that I would like to run through the args,kwargs command.</p>

<pre><code>import numpy as np
def moving_average(data,lookback=7,SMA=True): #MA function
    if SMA==True:
        weights=np.repeat(1.0,lookback)/lookback
        smas=np.convolve(data,weights,'valid')
        return smas
</code></pre>

<p>Just running this function works as expected.</p>

<pre><code>data=np.random.randn(100) #randomly 
moving_average(data,lookback=7,SMA=True) #this outputs the correct set of numbers
</code></pre>

<p>However the second I try to add args and kwargs it breaks down.</p>

<pre><code>def test1(*args,**kwargs):
    return moving_average(data,lookback,SMA)
test1(data,lookback=7,SMA=True) #this returns an error, saying my global lookback is not defined
</code></pre>

<p>What exactly in the *args **kwargs logic am I getting wrong? Ive tried inputting both a tuple and a dictionary but neither of those seem to work.</p>

<p>Pass the <code>*args</code> and <code>**kwargs</code> to your function not the argument(s) and named argument(s):</p>

<pre><code>def test1(*args,**kwargs):
    return moving_average(*args, **kwargs)
</code></pre>

python args kwargs