1

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.

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

Just running this function works as expected.

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

However the second I try to add args and kwargs it breaks down.

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

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.

1
  • 5
    That's... not what args and kwargs is for. If you know the argument names, and you just want to pass them on, why do you want to use args/kwargs? Commented Sep 20, 2016 at 14:29

4 Answers 4

2

Pass the *args and **kwargs to your function not the argument(s) and named argument(s):

def test1(*args,**kwargs):
    return moving_average(*args, **kwargs)
Sign up to request clarification or add additional context in comments.

Comments

2

In your example using *args and **kwargs:

def test1(*args,**kwargs):
    return moving_average(data,lookback,SMA)

data, lookback and SMA are no longer defined. It could be:

def test1(*args, **kwargs):
    return moving_average(args[0], kwargs['lookback'], kwargs['SMA'])

or

def test1(*args, **kwargs):
    return moving_average(*args, **kwargs)

The Python tutorial has a section that might help: Unpacking Argument Lists

Comments

2
def test1(*args,**kwargs):

Your function now has two local variables, args and kwargs. One holds the positional arguments that were passed in (as a tuple), the other the keyword arguments (as a dictionary).

return moving_average(data,lookback,SMA)

Here you use three variable names (data, lookback and SMA) that don't exist in your function, so you get an error.

You could have done

return moving_average(args[0], kwargs['lookback'], kwargs['SMA'])

But then your test1 function will only work with an exact call like test1(data,lookback=7,SMA=True). A call like test1(data, 7, True) won't work, as then the parameters are all in args, none in kwargs).

Your function could also pass on the parameters exactly as it received them:

return moving_average(*args, **kwargs)

That works, but there's no benefit to the test1 function, it just calls moving_average and returns its results, why not call moving_average directly.

Comments

-2

You can understand args and kwargs in python at PyMentor. Hope it helps you understand.

how to understand args and kwargs in python

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.