Calling python function with an unknown number of arguments [duplicate] - python

This question already has answers here:
Pass a list to a function to act as multiple arguments [duplicate]
(3 answers)
Closed 9 years ago.
I am writing a little script in which I call itertools.product like so:
for p in product(list1,list2,list3):
self.createFile(p)
Is there a way for me to call this function without knowing in advance how many lists to include?
Thanks

You can use the star or splat operator (it has a few names): for p in product(*lists) where lists is a tuple or list of things you want to pass.
def func(a,b):
print (a,b)
args=(1,2)
func(*args)
You can do a similar thing when defining a function to allow it to accept a variable number of arguments:
def func2(*args): #unpacking
print(args) #args is a tuple
func2(1,2) #prints (1, 2)
And of course, you can combine the splat operator with the variable number of arguments:
args = (1,2,3)
func2(*args) #prints (1, 2, 3)

Use the splat operator(*) to pass and collect unknown number of arguments positional arguments.
def func(*args):
pass
lis = [1,2,3,4,5]
func(*lis)

Related

Iterating through a tuple using a function and *args [duplicate]

This question already has answers here:
Python for loop returns True after first item
(4 answers)
Closed 3 years ago.
I'm trying to make a function using *args. It should take in certain numbers and then output the products of the numbers multiplied by 10.
I'm learning to use *args but I'm having trouble getting the output. My result only outputs the first number in the function. I've tried using a for loop, thinking it would iterate over each character, but it didn't work.
def myfunc(*args):
for args1 in args:
return args1*10
When I run the function:
myfunc(10.1,20)
The expected output should be
(101.0,200.0)
The actual output is
(101.0)
Any help would be appreciated. Thanks.
If I understand you, that's what you need:
def myfunc(*args):
return tuple(map(lambda x: x*10, args))
*args can be retrieved in the function as a list called args. What you do here is just multiplying the first element by 10 and return it, so the function stops.
You should use yield instead of return, to make your function return a generator, which can then be converted to tuple whenever you need
>>> def myfunc(*args):
... for args1 in args:
... yield args1*10
...
>>>
>>> myfunc(10.1,20)
<generator object myfunc at 0x7f20d1ae8ad0>
>>>
>>> tuple(myfunc(10.1,20))
(101.0, 200)

mix * argument with regular argument [duplicate]

This question already has answers here:
Unpacking arguments: only named arguments may follow *expression
(6 answers)
Closed 3 years ago.
I have the following code to use * argument for a function.
inputs = [fill(input) for input in input_shapes]
# num_runs is an integer
def bench(num_runs):
inputs.append(num_runs) # can I remove this?
handle_type(*inputs)
del inputs[-1] # can I remove this?
I want to know a better way to handle *inputs, so I don't have to call append and del every time.
I tried a simple example:
inputs = [1, 2, 3]
inputs2 = [1, 2]
def Test(a, b, c):
print (a, b, c)
Test(*inputs) # works
Test(*inputs2, 3) # SyntaxError: only named arguments may follow *expression
I found a solution here Unpacking arguments: only named arguments may follow *expression and it is what I am looking for.
You should be able to do this, at least in reasonably recent Python 3 versions:
inputs = [fill(input) for input in input_shapes]
def bench(num_runs):
handle_type(*inputs, num_runs)

How can I unpack a list or tuple in place in Python? [duplicate]

This question already has answers here:
Pass a list to a function to act as multiple arguments [duplicate]
(3 answers)
Closed 9 years ago.
Is it possible to unpack a list or tuple such that the values can be used as function parameters? I.e., how can I make the second line work?
f(1,2,3)
f(???([1,2,3]))
I'm currently doing this by hand, something like:
tmp1, tmp2, tmp3 = [1,2,3]
f(tmp1,tmp2,tmp3)
context: I don't have the ability to modify f(), but this is part of a code generator so funky solutions are not a problem.
Yes, you can use the *args (splat) syntax:
f(*[1, 2, 3])
See the call expressions documentation.
There is a keyword-parameter equivalent too, using two stars:
kwargs = {'foo': 'bar', 'spam': 'ham'}
f(**kwargs)
and there is equivalent syntax for specifying catch-all arguments in a function signature:
def func(*args, **kw):
# args now holds positional arguments, kw keyword arguments

I don't get how tuple unpacking works (Python-2.x) [duplicate]

This question already has answers here:
Use of *args and **kwargs [duplicate]
(11 answers)
Closed 9 years ago.
as I was wondering how tuple unpacking was working, I've found on several threads this answer as an alternative to slicing :
>>>>def unpack(first,*rest):
return first, rest
which works as follows:
>>>>first,rest=unpack(*(1,2,3))
>>>>first
1
>>>>rest
(2,3)
I don't understand how the * works. The argument "first" isn't supposed to be given to my function unpack ? I thought * meant that the argument was optional.
Thanks for your help
* in a function definition doesn't mean optional; it means "pack any additional (non-keyword) arguments the caller supplied into a tuple and put the tuple here". Similarly, * on a function call means "unpack this sequence of things and supply all the elements as arguments to the function individually."
unpack(*(1,2,3))
unpacks (1,2,3) and calls
unpack(1,2,3)
1 is assigned to first, and the remaining arguments 2 and 3 are packed into a tuple and assigned to rest.
In your case, unpack(*(1,2,3)) is just unpack(1, 2, 3).
Implementation of unpack takes the first argument, and an args tale, then returns it as a tuple.
Star syntax is useful, if you would pass arguments as a variable:
a = (1, 2, 3)
first, rest = unpack(*a)

Calling itertools.product with unkown number of args [duplicate]

This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 9 years ago.
I hope this is a easy one.
Is there any possiblity to call itertools.product with a yet (not hardcoded) not knwon number of arguments?
Something like this:
itertools.product(x[0],x[1],x[2],....)
and the dimension of x can't be hardcoded
Thanks!
Try:
itertools.product(*x)
i.e. we unpack the argument list.
You can use
itertools.product(*x)
Lookup *args and **kwargs?
a = [1,2,3]
b = [2,3,4]
c= [a,b]
itertools.product(*c)
You can pass array of arguments using *

Categories