1

Consider this minimal example:

def arfunc(x,*a):
    sa=a[1]
    return sa

b=arfunc(1.,(5.,2.))
print(b)

I need to pass an array whose length may vary to a function and then work with individual array elements within that function. If I exchange a[1] by a[0] in the function, the entire list is returned. Why does this not work, and how do I make it work?

4
  • Could you please more elaborate? what is x? Running the current code returns an error... Commented Mar 4, 2019 at 13:34
  • 1
    (5.,2.) is not an array, it is a tuple. Commented Mar 4, 2019 at 13:34
  • Not sure if I understood your question correctly, but see here: What does ** (double star/asterisk) and * (star/asterisk) do for parameters?. It says: "The *args will give you all function parameters as a tuple." You can add a print statement before the sa=a[1] line, and you will see that a == ((5., 2.),) - a tuple of one element which is also a tuple. Commented Mar 4, 2019 at 13:35
  • 2
    Possible duplicate of What does ** (double star/asterisk) and * (star/asterisk) do for parameters? Commented Mar 4, 2019 at 13:36

5 Answers 5

3

A function taking variable arguments (*args) expects them as several arguments:

b=arfunc(1., 5., 2.)
print(b)

If you want to use the function as you did, remove the asterisk:

def arfunc(x, a):
    ...
Sign up to request clarification or add additional context in comments.

3 Comments

This example is actually a test for a call of scipy.optimize.curve_fit (docs.scipy.org/doc/scipy/reference/generated/…), where the initial guesses p0 are passed as a tuple and the function must have the form f(xdata, *params). If I pass the tuple p0, I can't access its elements in the function the way shown in the example; if I omit the * in the def, I cannot pass more than one additional argument in params. The question stackoverflow.com/questions/54974948/… shows my problem in context.
So you have to use this signature and call, but only care about the first element? Just do a = a[0] inside the function body.
No, I care about all of them. If you look at the definition of the function pipoly at the beginning of that other thread (stackoverflow.com/questions/54974948/…), you see that I try to loop through the elements of the tuple.
1

* symbol in function definition allows a function to have a variadic type. The variable a will become a collection of all of the positional arguments passed after the first one: x.

There is no need to use the * symbol to let a have a variable length. Lists already have that property in Python.

Comments

1

you missed one thing that is if we have to pass the variable length argument(called Arbitrary Arguments in python) with a function you need to add *(asterisk)

def arfunc(x,*a):   # here *a can hold argument that may vary in numbers.
    sa=a[1]
    return sa

b=arfunc(1.,*(5.,2.)) # here you also have to provide a arbitary arguments like this.
print(b)

For better understanding refer below written code:

def arfunc(x,*a):
    sa=a[1]
    return sa

d = 1.
e = (5.,2.)

b=arfunc(d,*e)
print(b)

Comments

0

You can pass list or list of list.

Eg

List -

 [1,2,3,4]

List of lists -

[1,2,3[4,5,6]]

If thats what you are trying to do.

Comments

0

The star '*' operator is used to unpack arguments from a list. It is similar to the varargs in C/C++ or Java when a function expects a random number of arguments. You cannot change the original list / array in this case.

However, if you need to pass an array on that you will change inside the function, and then returned it back, I recommend you to pass and array or list to the function without the unpacking operator *. Example:

def try_to_change_list_contents(the_list):
   print('got', the_list)
   the_list.append('four')
   print('changed to', the_list)

outer_list = ['one', 'two', 'three']

print('before, outer_list =', outer_list)
try_to_change_list_contents(outer_list)
print('after, outer_list =', outer_list)

You can take a look at this post too: topic

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.