I know that *args are used when you dunno how many args are being used inside the function call. However, what do I do when I want one group of *args to do X and one group of *args to do y?
Asked
Active
Viewed 862 times
-2
-
1You can't. There's only one set of *args per function. – Morgan Thrapp Nov 17 '16 at 14:10
-
can you expound? is it your function, what are you trying to do? what's 'one group of args'-a certain number or a certain time or something else? – depperm Nov 17 '16 at 14:12
-
group the args into two groups either before or after the function call. – Steven Summers Nov 17 '16 at 14:14
-
There are a few ways to do function calls. The two ways to define parameter-requirmenets off functions looks like this `def x(*args, **kwargs)` or `def x(x, y=None)`. And some example calls to functions can look like this: `x(1,2)`, `x(1, y=2)` or `x(1, *{y : 2}). Not sure what you need really.. – Torxed Nov 17 '16 at 14:14
-
`*args` is used when you want all arguments reduced to a single list. If you want one group of args to do X and one to do Y, then you presumably don't want all of your arguments reduced to a single list, therefore you do not want to use `*args`. – dsclose Nov 17 '16 at 14:17
2 Answers
1
You can not pass two *args
within the single function. You need to pass args1
and args2
as plain list
and you may pass these lists as args to the functions doing X
and Y
. For example:
def do_X(*args):
# Do something
def do_Y(*args):
# Do some more thing
def my_function(list1, list2):
do_X(*list1) # Pass list as `*args` here
do_Y(*list2)
And your call to my_function
would be like:
args_1 = ['x1', 'x2'] # Group of arguments for doing `X`
args_2 = ['y1', 'y2'] # Group of arguments for doing `Y`
# Call your function
my_function(args_1, args_2)

Moinuddin Quadri
- 46,825
- 13
- 96
- 126
0
In that case you take the arguments as two separate lists, which can default to an empty list or None.

John Zwinck
- 239,568
- 38
- 324
- 436