0

I have a function that takes as input *args:

def foo(*args): 
    for a in args:
        print(a)

This works as intended

> foo('a', 'b')
> a
> b

I am faced with a situation where I don't know how many args are going to be passed (which is the whole point of args), but I don't know how to create a "list of positional arguments" to be passed to the function.

Part of the challenge is to appropriately phrase the question..

Let me try with an example: the function may be called as foo('a') or it may be called as foo('a', 'b', 'c', 'd', 'e') but I don't know because this function is being called by another class that creates the list of unknown number of parameters.

Essentially I want to create a variable containing the parameters like so:

positional_arguments = ('a','b','c')

so that the function can be called like foo(positional_arguments).

This however does not work as it does not correctly unpack positional_arguments:

> foo(positional_parameters)
> ('a', 'b', 'c')

but what I want is

> foo(positional_parameters)
> a
> b
> c
gmarais
  • 1,801
  • 4
  • 16
  • 32
  • 2
    `foo(*positional_parameters)`? This isn't a problem with the function, you're *passing* them wrongly. – jonrsharpe Apr 13 '20 at 13:10
  • I don't recall saying that the function had a 'problem', I was merely stating what I have tried and was asking how to do it properly. – gmarais Apr 13 '20 at 13:19

0 Answers0