3

I know you can use *args in Python to allow a tuple or arguments. But how could one have two of these. Like *args and *args1?

Is this possible?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
  • Then what do you want to get? What do you want `*args1` be? – Remi Guan Jan 14 '16 at 05:24
  • Also if you just try it, you can get a `SyntaxError: invalid syntax`. – Remi Guan Jan 14 '16 at 05:25
  • How would you differentiate `*args` and `args1` you can't. So as jim stated **no** – The6thSense Jan 14 '16 at 05:34
  • You probably need to read up on python function arguments in more detail for what these mean. The short answer, though, is that when you see a function defined like `def f(*args)` python just takes all extra positional arguments and creates a tuple to hold them. If you want a function taking multiple tuples you can explicitly define such like `f(x1,x2)` where `x1` and `x2` are tuples. The star-ed form is simply for when you need any number of arguments and can't specify how many before hand. It doesn't mean a tuple, but "everything I haven't specifed". – Matthew Jan 14 '16 at 05:47
  • What are you trying to do with such a syntax? Maybe there is a better way. – Burhan Khalid Jan 14 '16 at 05:57

3 Answers3

2

I assume you mean in terms of function arguments, then no it isn't possible. The reason why is a tuple *args can be of any length of 0 or more arguments. If you had another one, *args2, how would you determined which arguments belong to *args and which to *args2? You can however include a **kwargs which is a dictionary object of keyword arguments. For example:

def my_function(x, *args):
    ...

You can figure out what the args of *args are. However, in

def my_function2(x, *args, *args):
    ...

You cannot determine which arguments go into args1 and which go into *args2. However, for

def my_function3(x, *args, **kwargs):
    ...

It's possible to differentiate the arguments that belong to *args and those that belong to **kwargs because the arguments that belong to the **kwargs take the form of arg = val.

beigel
  • 1,190
  • 8
  • 14
2

You can't have multiple variadic parameters of the same type in a function definition (e.g. def func(*args, *args1), but, in Python 3.5, you can pass arguments in that form when calling a function.

Python 3.4:

>>> print(*range(3), *range(3))
  File "<stdin>", line 1
    print(*range(3), *range(3))
                     ^
SyntaxError: invalid syntax

Python 3.5:

>>> print(*range(3), *range(3))
0 1 2 0 1 2
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • I see now. Kwargs is a dictionary. Okay. Meaning I can run – user2254230 Jan 14 '16 at 07:25
  • "Kwargs" isn't a dictionary - any name preceded by `**`, syntax permitting, represents an unpacked dictionary. See [here](http://stackoverflow.com/questions/287085/what-do-args-and-kwargs-mean?rq=1) and [here](http://stackoverflow.com/questions/3394835/args-and-kwargs?rq=1) for more. – TigerhawkT3 Jan 14 '16 at 07:29
  • I see now. Kwargs is a dictionary. Okay. Meaning I can run: `def swag(**kwargs): print(kwargs["name"]` with `swag(name="Tristan")` which will print out Tristan then. – user2254230 Jan 14 '16 at 08:34
  • Oh okay. So I can do **swag and use it as a dictionary. What is an unpacked dictionary. Does ** make it a dictionary without having to do d = {} – user2254230 Jan 14 '16 at 08:36
  • Yes, that `swag()` example would work (I encourage you to experiment with the interpreter). An unpacked dictionary looks like `swag(name='Tristan')`, which is different from sending in an actual dictionary with `swag({'name':'Tristan'})`. – TigerhawkT3 Jan 14 '16 at 08:39
0

It is not possible to pass *args twice when calling a function (up until in Python 3.5)

>>> fun(*a, *b)
  File "<stdin>", line 1
    fun(*a, *b)
            ^
SyntaxError: invalid syntax    

However, what you can do is concatenate two args (list or tuple) as you pass them using +. For example:

>>> def fun(*args):
...    print(args)
...
>>> a = [1,2,3,4,5]
>>> b = [6,7,8,9]
>>>
>>> fun(*a)
(1, 2, 3, 4, 5)
>>> fun(*b)
(6, 7, 8, 9)
>>> fun(*a+b)
(1, 2, 3, 4, 5, 6, 7, 8, 9)

The function definition can only have a single *args definition, and all passed position arguments will end up in the args variable in the order they were passed.

mfitzp
  • 15,275
  • 7
  • 50
  • 70