boop
boop

Reputation: 7788

Set *args from variable?

Is it possible to set *args from a variable?

def fn(x, *args):
    # ...

# pass arguments not as list but each as single argument
arguments = ??? # i.e.: ['a', 'b']


fn(1, arguments)

# should be equivalent to
fn(1, 'a', 'b')

Upvotes: 0

Views: 381

Answers (2)

Wang Xiaoyu
Wang Xiaoyu

Reputation: 387

# pass arguments not as list but each as single argument
arguments = ??? # i.e.: ['a', 'b']

Then arguments should be assigned arguments = ['a' ,'b']. This is argument unpacking.

Upvotes: 0

user2555451
user2555451

Reputation:

Yes, you can use argument unpacking (also known as splatting):

fn(1, *arguments)

Below is a demonstration:

>>> def fn(x, *args):
...     return args
...
>>> arguments = ['a', 'b']
>>> fn(1, *arguments)
('a', 'b')
>>>

Upvotes: 5

Related Questions